2013-05-29 80 views
1

我有一個看起來像這樣的順序: dummy order編程方式創建出貨(對選定的項目/訂單數量)在Magento

我想創建此訂單以下(不是一切)裝運:

2×VCF001

1×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

回答

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

嗨,在這段代碼哪一點你指定的產品,我想出貨數量?看起來它會爲訂單上的所有東西創造出貨。 – Latheesan

+0

Yep添加了數量,對不起:看看'Mage_Sales_Model_Order'中的'prepareShipment($ qtys = array())'方法調用'Mage_Sales_Model_Service_Order :: prepareShipment($ qtys = array())' –

+0

我試過了沒有運氣。我修改後的代碼:http://pastebin.com/f2TJKF4N - 獲取完全相同的錯誤**無法創建空貨** - http://pastebin.com/raw.php?i=k9gnCaSx – Latheesan

相關問題