1

我有一个看起来像这样的订单: 虚拟订单

我想为该订单中的以下物品(不是所有物品)创建一个货件:

2 个 VCF001

1 个 SBA364

为此,我在货件 api 上找到了以下信息: //bit.ly/17rpuwj

基于这个 API 文档,我想出了以下代码。当我尝试执行它时,我收到以下错误Cannot create an empty shipping

$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

4

1 回答 1

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;
    }
于 2013-05-29T11:48:18.070 回答