在magento中,如何添加货件和跟踪编号

时间:2011-07-11 14:33:03

标签: magento orders

我需要动态地将货件和货件跟踪添加到订单中,它需要动态,因为我们将批量执行此操作,您能给我一些帮助吗?

编辑:
用户将看到一个包含订单列表的页面,然后他将输入每个订单的跟踪编号并提交表单,因此我需要获得一个已知的运营商并通过该运营商发送所有订单。

2 个答案:

答案 0 :(得分:12)

如果您有订单ID列表和相应的跟踪号码,

$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
$shipment_collection->addAttributeToFilter('order_id', $order_id);

然后您可以浏览所有货件并添加跟踪,例如

foreach($shipment_collection as $sc) {
    $shipment = Mage::getModel('sales/order_shipment');
    $shipment->load($sc->getId());
    if($shipment->getId() != '') { 
        $track = Mage::getModel('sales/order_shipment_track')
                 ->setShipment($shipment)
                 ->setData('title', 'ShippingMethodName')
                 ->setData('number', $track_no)
                 ->setData('carrier_code', 'ShippingCarrierCode')
                 ->setData('order_id', $shipment->getData('order_id'))
                 ->save();
    }
}

您需要在此代码之上设置一个订单ID和跟踪ID的嵌套循环。

答案 1 :(得分:3)

在这里你去:)。

    private function _createShipment($shipment, $itemsQty)
    {
        $itemsQtyArr = array();
        foreach ($itemsQty as $item)
        {
            $itemsQtyArr[$item->iExternalOrderId] = $item->dQtyShipped;
        }

        try
        {
            $shipmentIncrementId = Mage::getModel('sales/order_shipment_api')->create($shipment->sOrderNumber, $itemsQtyArr, $shipment->sShipmentComment, true, true);

            if ($shipmentIncrementId)
            {
                Mage::getModel('sales/order_shipment_api')->addTrack($shipmentIncrementId, $shipment->sCarrierCode, $shipment->sTrackingTitle, $shipment->sTrackingNumber);
            }
        }
        catch(Exception $e)
        {
            Mage::log('Exception: ' . $e->getMessage());
        }

        return $shipmentIncrementId ? true : false;
    }