如何在Magento中以编程方式将订单商品添加到现有货件?

时间:2013-01-05 00:22:34

标签: 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 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。在阅读了stackoverflow和Magento论坛后,我发现货件api应该支持单件货物的运输。但是,您的代码似乎不支持此功能。

也许我最好尝试使用Magento的货件API? http://www.magentocommerce.com/wiki/doc/webservices-api/api/sales_order_shipment

答案 1 :(得分: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);
        }