1

I have a scenario where I need to do some calculation on grand total.

  1. When order is placed successfully, fetch order status and do something
  2. After order is placed, and when order status is changed, fetch order status and do something more.

I am using the event sales_order_place_after while for second one sales_order_save_after.

The issue is for first case event sales_order_place_after is working good and calculations are correct. But for second case, I am not able to fetch order status using sales_order_save_after when order is being placed successfully.

Order status can be only retrieved when its changed from backend, while I need to get it after order is placed and/or order is changed.

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.

Am I using incorrect events in this case ?

Thanks

3

Override magento Mage_Sales_Model_Order using or Copy

app/code/core/Mage/Sales/Model/Order.php

to

app/code/local/Mage/Sales/Model/Order.php

add define new event on _setStatus function

    protected function _setState($state, $status = false, $comment = '',
    $isCustomerNotified = null, $shouldProtectState = false)
{
Mage::dispatchEvent('sales_order_status_before', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));
    // attempt to set the specified state
    if ($shouldProtectState) {
        if ($this->isStateProtected($state)) {
            Mage::throwException(
                Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state)
            );
        }
    }
    $this->setData('state', $state);

    // add status history
    if ($status) {
        if ($status === true) {
            $status = $this->getConfig()->getStateDefaultStatus($state);
        }
        $this->setStatus($status);
        $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
        $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
    }
Mage::dispatchEvent('sales_order_status_after', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));
    return $this;
}

using two sales_order_status_after and sales_order_status_before you can do your requirement

or best way to do this sales_order_save_after and in this observer if

you get old value then try

$oldstatus=$order->getOrigData('status');
$Newstatus=$order->getData('status');

https://stackoverflow.com/questions/8182009/get-product-changes/8184430#8184430

| improve this answer | |
  • Does the event sales_order_save_after gives me order status ? – Jordan Aug 25 '14 at 10:40
  • yes.. can you already try this? – Amit Bera Aug 25 '14 at 10:42
  • $order = $observer->getEvent()->getOrder(); print_r($order->getData()) takes forever to show results. – Jordan Aug 25 '14 at 10:43
  • Try $order->getStatus(); – Amit Bera Aug 25 '14 at 10:51
  • The event is called when order is being placed. It will give the status after the order is placed successfully and when status is changed from admin, and not while being placed. – Jordan Aug 25 '14 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.