能够以编程方式创建Magento货件但无法将其标记为已发货吗?

时间:2014-03-31 15:26:06

标签: php magento

我有以下代码可以为订单创建货件。但是仍然没有将已发货的商品标记为已发货。顶部的SHIP按钮仍然存在。因此,如果需要,我无法创建RMA。

请检查屏幕截图(link

$order = Mage::getModel('sales/order') -> loadByIncrementId($order_id);
$itemQty = $order -> getItemsCollection() -> count();

$convertOrder = new Mage_Sales_Model_Convert_Order();
$shipment = Mage::getModel('sales/service_order', $order) -> prepareShipment($itemQty);

$items = $order -> getAllItems();

foreach ($items as $item) {
    $shipped_item = $convertOrder -> itemToShipmentItem($item);
    $shipped_item -> setQty($item -> getQtyOrdered());
    $shipment -> addItem($shipped_item);
}

$shipment -> register();
$shipment -> setOrder($order);
$shipment -> save();

1 个答案:

答案 0 :(得分:1)

我总是跳到这些问题的源头。确定该按钮是否显示的代码在此处

    #File: app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php
    if ($this->_isAllowedAction('ship') && $order->canShip()
        && !$order->getForcedDoShipmentWithInvoice()) {
        $this->_addButton('order_ship', array(
            'label'     => Mage::helper('sales')->__('Ship'),
            'onclick'   => 'setLocation(\'' . $this->getShipUrl() . '\')',
            'class'     => 'go'
        ));
    }

考虑到这一点,最好的选择是canShip方法

#File: app/code/core/Mage/Sales/Model/Order.php
public function canShip()
{
    if ($this->canUnhold() || $this->isPaymentReview()) {
        return false;
    }

    if ($this->getIsVirtual() || $this->isCanceled()) {
        return false;
    }

    if ($this->getActionFlag(self::ACTION_FLAG_SHIP) === false) {
        return false;
    }

    foreach ($this->getAllItems() as $item) {
        if ($item->getQtyToShip()>0 && !$item->getIsVirtual()
            && !$item->getLockedDoShip())
        {
            return true;
        }
    }
    return false;
}

在此处删除一些var_dump / Mage::log调试,您应该能够弄清楚为什么Magento认为需要显示发货按钮。一旦你知道了,你应该能够找出你需要保存的其他状态。