Magento如何自动为订单添加跟踪号

时间:2013-04-29 10:38:12

标签: magento

我想自动将发票的最后4位数字(如果存在)添加为每个订单的跟踪编号。我怎么做?感谢

1 个答案:

答案 0 :(得分:2)

创建货件时,使用自定义模块的观察者动态分配跟踪编号。见下文。

config.xml中

<sales_order_shipment_save_before>
    <observers>
        <namespace_modulename_ship_before>
            <type>singleton</type>
            <class>Namespace_Modulename_Model_Observer</class>
            <method>salesOrderShipmentSaveBefore</method>
        </namespace_modulename_ship_before>
    </observers>
</sales_order_shipment_save_before>

Observer.php

public function salesOrderShipmentSaveBefore($observer)
{
    $invoice = $observer->getEvent()->getInvoice();
    $last4 = substr($invoice->getIncrementId(), -4);

    $shipment = $observer->getEvent()->getShipment();
    $track = Mage::getModel('sales/order_shipment_track')
                ->setNumber($last4) //tracking number / awb number
                ->setCarrierCode('custom') //carrier code
                ->setTitle('Custom'); //carrier title
    $shipment->addTrack($track);
}

信用:http://ka.lpe.sh/2012/01/08/magento-save-shipment-information-tracking-number-carrier-code-programatically/