In magento, how to add shipment and track number to order

点点圈 提交于 2019-12-06 03:47:10

问题


I need to dynamically add a shipment and shipment track into an order, it needs to be dynamically because we will do it in batch, can you guys give me some help on this?

EDIT:
The user will see a page with a list of orders, then he will input the track number for each and submit the form, so I need to get a known carrier and send all orders via this carrier.


回答1:


If you have a list of order ids and corresponding tracking numbers you can,

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

Then you can go through all the shipments and add the tracking like,

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();
    }
}

You would need to have a nested loop of order ID and tracking ID on top of this code.




回答2:


here you go :)

    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;
    }  


来源:https://stackoverflow.com/questions/6651725/in-magento-how-to-add-shipment-and-track-number-to-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!