2012-01-29 79 views
7

我在看一些代碼,在Magento和船舶和完整的多個訂單增加MassActionsales_order/indexMagento如何以編程方式發送訂單?

莫名其妙的命令沒有被運。

它看起來像(完全正常的順序)沒有通過canship()測試。是否應通過$orderid傳遞$order

這裏是我的代碼

//Get orderids 
$orderIds = $this->getRequest()->getPost('order_ids'); 

//verify if the array is not empty 
if (!empty($orderIds)) { 
//loop through orders 
foreach ($orderIds as $orderId) { 

// Dont know what this does 
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId); 

// Is the order shipable? 
if($order->canShip()) 
{ 
$itemQty = $order->getItemsCollection()->count(); 
// This first definition and 2nd look overlapping, our one is obsolete? 
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty); 
$shipment = new Mage_Sales_Model_Order_Shipment_Api(); 

// But still, no shipment, why? 
$shipmentId = $shipment->create($orderId, array(), 'Shipment created through ShipMailInvoice', true, true); 

回答

6

您需要通過ID來加載,如果你單編號,或負載由IncrementOrderId如果,你實際上得到訂單incrementId。

使用此:

$order = Mage::getModel('sales/order')->load($orderId);

讓我們知道它的工作。

然後:

$shipmentId = $shipment->create($order->getIncrementId(), $itemQty, 'Shipment created through ShipMailInvoice', true, true); 

嘗試。

+0

非常感謝@ShaunOReilly。它通過canship()... 但出現下一個錯誤:order_not_exists 'code' 跟蹤:訂單/出貨/ Api.php(142):Mage_Api_Model_Resource_Abstract - > _故障( 'order_not_exist ......') 銷售/ OrderController.php(45):Mage_Sales_Model_Order_Shipment_Api->創建( '121',陣列, '出貨創造... ...',真,真) 'code' – 2012-01-30 19:50:31

+0

所以現在它崩潰上$ shipment->創建。我嘗試了$ order和orderId。兩者都不起作用。 $ order導致訪問衝突 $ shipmentId = $ shipment-> create($ order,array(), – 2012-01-30 19:57:44

+0

看看我的答案的最後編輯。 – ShaunOReilly 2012-01-30 23:52:49

1
$order = Mage::getModel('sales/order')->load($orderId); 

//create shipment 
$itemQty = $order->getItemsCollection()->count(); 
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty); 
$shipment = new Mage_Sales_Model_Order_Shipment_Api(); 
$shipmentId = $shipment->create($order->getIncrementId(), array(), 'Shipment created through ShipMailInvoice', true, true); 

//add tracking info 
$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection'); 
$shipment_collection->addAttributeToFilter('order_id', $orderId); 
foreach($shipment_collection as $sc) 
{ 
$shipment = Mage::getModel('sales/order_shipment'); 
$shipment->load($sc->getId()); 
           if($shipment->getId() != '') 
           { 
           try 
           { 
            Mage::getModel('sales/order_shipment_track') 
            ->setShipment($shipment) 
            ->setData('title', 'carrier') 
            ->setData('number', $trackInfo) 
            ->setData('carrier_code', 'custom') 
            ->setData('order_id', $shipment->getData('order_id')) 
            ->save(); 

           }catch (Exception $e) 
           { 
            Mage::getSingleton('core/session')->addError('order id '.$orderId.' no found'); 
           } 
           } 
         } 
// change order status to complete 
         $order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE); 
         $order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE); 
         $order->save(); 

任何想要創建船員的工作代碼,2.添加運輸跟蹤信息和3.更改船舶狀態以完成。

非常感謝ShaunOReilly。

相關問題