Magento – Hook into “order shipped” event and get order id for order shipped

event-observermoduleshipping

I have created a module that hooks into the

sales_order_shipment_save_after

event. I have tested the hook is working with Mage::log, My question is, how do I get the order id for the shipment that was just created?
Edit: I tried the following, but $orderId is returning blank in the log…

public function invoice_and_complete($observer)
{
   $event = $observer->getEvent();
   $orderId = $event->getOrderIds();

   Mage::log($orderId);
}

Best Answer

For sales_order_shipment_save_after it looks as if it only passes the shipment to you in the getEvent method. From there you should be able to get to the order object - An example:

class Company_Shipmentsave_Model_Observer
{
    public function salesOrderShipmentSaveAfter(Varien_Event_Observer $observer)
    {
            $shipment = $observer->getEvent()->getShipment();
            $order = $shipment->getOrder();

            //do something with order - get the increment id:
            $order->getIncrementId();

            //get all of the order items:
            $items = $order->getAllItems();
            return $this;
    }

}

Source:

https://stackoverflow.com/questions/3164800/my-magento-observer-is-getting-in-endless-loop

Related Topic