I would like to automatically add the last 4 digits of invoices, when it exists, to be a tracking number for every order. How do I do that? Thanks
            Asked
            
        
        
            Active
            
        
            Viewed 5,309 times
        
    1 Answers
2
            When you create a shipment, assign the tracking number dynamically using your custom module's observer. See below.
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);
}
 
    
    
        Kalpesh
        
- 5,635
- 2
- 23
- 39
- 
                    Could you perhaps give me a link on how to create modules :| still need to work with that – HWR Apr 29 '13 at 12:18
- 
                    here you will need 3 files.. one in app/etc/modules/ for registering your custom module, second in your local/Namespace/Module/etc/ i.e. config.xml i wrote above, third in local/Namespace/Module/Model/ your Observer i wrote above.. link to learn creating module http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in-magento – Kalpesh Apr 29 '13 at 13:09
