How get order status in observer?

Asif hhh picture Asif hhh · Sep 12, 2012 · Viewed 9.3k times · Source

Under the event sales_order_save_after, I have some code in observer to get the order status.

public function saveBookingInfoFromOrder(Varien_Event_Observer $observer){                
        $order = $observer->getEvent()->getOrder();

        //$last_orderid = $order->getIncrementId();

        $last_orderid = $order->getId();
        $order_status = $order->getStatus();
}

However, order status is not coming. Why?

Answer

Slayer Birden picture Slayer Birden · Sep 12, 2012

Event sales_order_save_after is a bit tricky. The first time it's triggered Order is not yet commited to DB, and more to that - it actually doesn't have State and Status set yet.

If you want to get Status, there are other events more suitable for you:

  1. sales_order_payment_place_end - this is triggered in Mage_Sales_Model_Order_Payment::place method right after $order->setState is done. The place function itself is a afterCommit callback for order saving transaction process.

  2. sales_order_save_commit_after - this is triggered when all afterCommits are processed

  3. sales_model_service_quote_submit_success - this is the final event in Mage_Sales_Model_Service_Quote::submitOrder method - at the moment it's dispatched the Order will surely have all the data, and you can be sure that there were no errors during order creation.