【问题标题】:Programatically create shipment (for selected items/qty on order) in magento在 magento 中以编程方式创建货件(针对选定的项目/订单数量)
【发布时间】:2013-05-29 11:14:29
【问题描述】:

我的订单如下所示:

我想为该订单上的以下商品创建发货(不是所有商品):

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

【问题讨论】:

    标签: magento


    【解决方案1】:

    试试这样吧:

        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;
        }
    

    【讨论】:

    • 您好,您在此代码中的哪个位置指定了我要运送的确切产品和数量?这看起来会为订购的所有物品创建一个装运。
    • 是的,添加了数量,抱歉:看看Mage_Sales_Model_Order 中的prepareShipment($qtys = array()) 方法调用Mage_Sales_Model_Service_Order::prepareShipment($qtys = array())
    • 我试过了,但仍然没有运气。我修改后的代码:pastebin.com/f2TJKF4N - 得到完全相同的错误 无法创建空货件 - pastebin.com/raw.php?i=k9gnCaSx
    • 看看我提到的功能。您会看到您使用的 $qty 数组是错误的:您需要一个类似的数组:$array[$productId] = $qty (entityId, not SKU, and single dimesnionnal)
    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多