以编程方式在magento中创建货件(针对选定的商品/订单数量)

时间:2013-05-29 11:14:30

标签: magento

我的订单如下: dummy order

我想在此订单上为以下内容创建货件(不是为了所有货物):

  

2 x VCF001

     

1 x SBA364

为实现这一目标,我在货件api上找到了以下信息:http://bit.ly/17rpuwj

根据API的这个文档,我提出了以下代码。当我尝试执行它时,出现以下错误无法创建空货

$order_number = '300000002';
$order = Mage::getModel('sales/order')->loadByIncrementId($order_number);
$allocations = array(
    array('sku' => 'VCF001', 'allocated_qty' => 2),
    array('sku' => 'SBA364', 'allocated_qty' => 1)
);

function GetOrderItemId($order_items, $sku) {
    foreach ($order_items as $order_item) {
        if ($order_item->getSku() == $sku) {
            return $order_item->getItemId();
        }
    }
    return 0;
}

function CreateShipment($order, $allocations)
{
    // Get Order Items
    $order_items = $order->getItemsCollection();

    // Parepare Item Qtys For Shipment
    $item_qtys = array();
    foreach ($allocations as $allocation) {
        $item_qtys[] = array(GetOrderItemId($order_items,
            $allocation['sku']) => $allocation['allocated_qty']);
    }

    // Anticipate Errors
    try
    {
        // Create Shipment
        $shipment = new Mage_Sales_Model_Order_Shipment_Api();
        $shipmentId = $shipment->create($order->getIncrementId(),
            $item_qtys, 'Pick #123 Sent to Warehouse');

        // Done
        echo 'Shipment Created - ID #'. $shipmentId;
    }
    catch (Exception $e)
    {
        print_r($e);
    }
}

CreateShipment($order, $allocations);

知道为什么这不按预期工作?请在此处查看完整的例外日志:http://pastebin.com/raw.php?i=GMN4WCAE

1 个答案:

答案 0 :(得分:4)

请尝试这样:

    protected function _createShipment($order, $qtysForProducts)
    {
        $shipment = $order->prepareShipment($qtysForProducts);
        if ($shipment) {
            $shipment->register();

            $shipment->sendEmail(true)
            ->setEmailSent(true)
            ->save();

            $order->setIsInProcess(true);

            $transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
        }

        return $shipment;
    }
相关问题