Magento – Observing order state change

event-observerordersstate

It seems there is no order state event by default, so how do you usually go about observing order state change?
There are two possibilities I see:
a) Overwrite the sales/order model to create your custom event for a state change
b) Observe the order save event and look at the state in there

Is one of these the way to go or is there a better option?

Edit:
Thanks for your answers so far.
The situation is a bit more complicated. I want to add information to the order depending on the current state. Now here is the problem:
In the save_before event I still have the old state and do not now the new state, because in the sales/order model the _beforeSave() method is as follows:

parent::_beforeSave();
$this->_checkState();
//...

So the event is processed in parent::_beforeSave(); BUT the order state is actually changed afterwards in $this->_checkState(); (this is the automatic change, e.g. if you create an invoice, the state is moved to processing if no shipment is there, yet)

I cannot use the save_after event either, since I want to save sth. at the order and it would probably break everything to call a save in the save_after event.

Any ideas? My only idea now is to replicate the $this->_checkState(); behavior in my save_before observer to find out what the state will finally be…

Best Answer

I would strongly advise against overwriting sales/order model because of two reasons:

  1. It is always better to use events instead of rewrites.
  2. Besides the general advice to use events instead of rewrites, it is particularly not a good idea to overwrite such a central and important Magento class. The possibility for a conflict with another extension is too high.

I do not have any idea for a better solution - I think it would be absolutely okay to observe the sales_order_save_after event and check if the state has been changed.

Related Topic