1

我正在 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,但是当我单击时装运查看详细信息,两个项目都列出。所以看起来它有点工作,但不是真的

并非所有物品都会发货,所以我不能只加载订单并将它们全部标记为已发货。关于如何完成这项工作的任何想法?提前致谢 ;)

4

2 回答 2

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);
        }
于 2013-06-26T18:03:58.433 回答
0

我有同样的问题。在阅读了 stackoverflow 和 Magento 论坛后,我发现发货 API 应该支持单件商品的发货。但是,您的代码似乎不支持这一点。

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

于 2013-01-10T11:53:19.560 回答