Question

I am trying to retrieve the order_id of an order that has just been created during checkout process (after the place order button was clicked).

I have an observer listening to the sales_order_place_after event. I am trying to retrieve the order_id using this code :

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

which returns the order_id of the previous order and not the current order that was just created during the checkout. If I use something like this :

$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('sales/order')->getLastOrderId());
$lastOrderId = $order->getIncrementId();

I do not get the order_id.

The reason I am trying to do this is to set all the order status to hold or pending when they are created initially and when the user made a successful transaction, I will be changing the status to processing.

How do I retrieve the current order_id for the checkout session in progress within a observer?

Was it helpful?

Solution

If you inspect the code and grep for the event you are using:

grep 'sales_order_place_after' app/code -rsn
app/code/core/Mage/Sales/Model/Order.php:1074:        Mage::dispatchEvent('sales_order_place_after', array('order'=>$this));

then you see that order object is passed on to event and in your observer method you can do

public function myCreateOrderAfterObs($observer){
    $order = $observer->getEvent()->getOrder();
    $orderEntityId = $order->getId(); 
    $orderIncrementId = $order->getIncrementId();
    //print_r($order->getData());
}

You can request everything that is passed to observer and I encourage you to use only objects that are passed and not some global or session items as they might not be available in your observer context or at the time observer is invoked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top