Jan 8, 2012
kalpesh

Magento: Save shipment information of order programatically

After creating invoice and shipment, it is necessary to add tracking information to shipment. Here is how to write a observer which will invoke as shipment save method is called and save tracking information programatically.

config.xml – under global -> events node

<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 -> under Model directory of module

public function salesOrderShipmentSaveBefore($observer)
    {
            $shipment = $observer->getEvent()->getShipment();
            $track = Mage::getModel('sales/order_shipment_track')
                        ->setNumber('824343454454') //tracking number / awb number
                        ->setCarrierCode('aramex') //carrier code
                        ->setTitle('Aramex'); //carrier title
                    $shipment->addTrack($track);
       }

5 Comments

  • Hi,

    Great blog !!! I tried to contact you about magento and possible partnershp, but your sendmail.php is missing 404, so the contact form does not work at all. Please drop me an email,

    thanks,

    IStvan

  • Replace
    $shipment = $observer->getEvent()->getShipment();

    with
    $shipment = $observer->getShipment();

  • Hi..

    Nice share.. your article works well for me..

    Btw I have problem, how to get shipping address, city etc?

    $observer->getInvoice(); //return empty

  • I trying the same code:

    public function salesOrderShipmentSaveBefore($observer)
    {
    $shipment = $observer->getEvent()->getShipment();
    $track = Mage::getModel(‘sales/order_shipment_track’)
    ->setNumber(‘824343454454’) //tracking number / awb number
    ->setCarrierCode(‘aramex’) //carrier code
    ->setTitle(‘Aramex’); //carrier title
    $shipment->addTrack($track);
    }

    But Carrier Code is changing to aramex it is always “custom value” …

  • I trying the same code:

    public function salesOrderShipmentSaveBefore($observer)
    {
    $shipment = $observer->getEvent()->getShipment();
    $track = Mage::getModel(‘sales/order_shipment_track’)
    ->setNumber(’824343454454′) //tracking number / awb number
    ->setCarrierCode(‘aramex’) //carrier code
    ->setTitle(‘Aramex’); //carrier title
    $shipment->addTrack($track);
    }

    But Carrier Code is not changing to aramex it is always “custom value” …

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions