2013-01-05 61 views
1

我從一個導入文件中創建Magento的貨件,該文件讀取將逐行運送的訂單商品,但在創建貨件並將我遇到的第一個項目添加到困難中後。我正在創建這樣的貨件:如何在Magento中以編程方式將訂單項添加到現有貨件中?

$itemsData = array(
    'qtys' => array(
     $orderItem->getId() => $importData['qty placed'], 
    ) 
); 
$shipment = Mage::getModel('sales/service_order', $order) 
    ->prepareShipment($itemsData['qtys']); 
$shipment->addComment("Shipment created from shipment importer", false); 
$shipment->register(); 
$shipment->getOrder() 
    ->setIsInProcess(true); 
$shipment->setShippingCost($shipCost); 
if (isset($trackingNumber)) { 
    $this->addTrack($shipment, $importData); //this function adds the tracking data 
} 
if ($order->getCustomerEmail()) { 
    $shipment->setEmailSent(true); 
    $shipment->sendEmail(true, ''); 
} 
$transactionSave = Mage::getModel('core/resource_transaction') 
    ->addObject($shipment) 
    ->addObject($shipment->getOrder()); 
$transactionSave->save(); 
$shipment->save(); 

這對第一件物品很好,但對於具有多件物品的貨件我無法將下一件物品添加到貨件。我想添加後續項目是這樣的:

//I first load the existing shipment 
$shipItem = Mage::getModel('sales/order_shipment_item'); 
$shipItem->setShipment($shipment); 
$shipItem->setOrderItem($orderItem); 
$shipItem->setQty($qtyShipped); 
$shipItem->register(); 

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

現在在管理員的順序查看/信息頁,第二項仍然說「發票」而不是「發貨」,當我點擊發運標籤的總數量列爲'1'而不是2,但是當我點擊該貨件查看詳細信息時,這兩項都列出。所以它似乎有點工作,但不是真的

並非所有項目都會發貨,所以我不能只加載訂單並將它們全部標記爲已發貨。 關於我如何完成這項工作的任何想法?在此先感謝;)

+2

我不知道了'銷售/ service_order'是什麼,通常使用'銷售/訂單' –

回答

0

從訂單

 $mageShipColl = $mageOrder->getShipmentsCollection(); 
     $mageShipment = $mageShipColl->getFirstItem(); 
     $mageShipment->setOrder($mageOrder) 
      ->setStoreId($mageOrder->getStoreId()) 
      ->setCustomerId($mageOrder->getCustomerId()) 
      ->setBillingAddressId($mageOrder->getBillingAddressId()) 
      ->setShippingAddressId($mageOrder->getShippingAddressId()); 
     Mage::helper('core')->copyFieldset('sales_convert_order', 'to_shipment', $mageOrder, $mageShipment); 

現在做這讓你現有的貨物模型:

$mageShipmentItem = $convertor->itemToShipmentItem($mageOrderItem); 
$mageShipmentItem->setQty($qty_you_shipped); 
//register shipment item for existing shipment only 
if($mageShipment->getId()) { 
    $mageShipmentItem->register(); 
} 
$mageShipment->addItem($mageShipmentItem); 

     $mageTrack = Mage::getModel('sales/order_shipment_track'); 
     try { 
      $mageTrack->setData('carrier_code', $carrier) 
       ->setData('number', $trackNo) 
       ->setData('title', $carrierTitle); 

      $comment = "Shipped via $carrier with tracking# $trackNo. Emailed confirmation to $shipToEmail."; 

      $mageShipment->addTrack($mageTrack); 
      //Do not register an existing shipment 
      if(!$mageShipment->getId()) { 
       $mageShipment->register(); 
      } 
      $mageShipment->addComment($comment, $shouldNotifyCust); 

      //this may or may not be needed 
      $mageShipment->getOrder()->setIsInProcess(true); 

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

      //email customer only if order is complete 
      if($mageOrder->getStatus() == "complete") { 
       $mageShipment->sendEmail(true, (($this->includeCommentInCustomerEmail) ? $comment : '')); 
       $mageShipment->setEmailSent(true); 
      } 

      $mageShipment->save(); 


     } catch(Exception $e) { 
      $errMsg = "Error saving shipment: " . $e->getMessage(); 
      Mage::log($errMsg, null, $logPath); 
     } 
+0

此外,您可能想要堅持@ FabianBlechschmidt的評論。 –

相關問題