How get order status in observer?
Frage
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?
Lösung
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:
sales_order_payment_place_end
- this is triggered inMage_Sales_Model_Order_Payment::place
method right after$order->setState
is done. Theplace
function itself is a afterCommit callback for order saving transaction process.sales_order_save_commit_after
- this is triggered when all afterCommits are processedsales_model_service_quote_submit_success
- this is the final event inMage_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.