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
As far as I know, we can use events: sales_order_save_after
and sales_order_state_change_before
. I refer sales_order_save_after
event.
In our observer, we can get the state of order:
Observer/SalesOrderAfterSave.php
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
if ($order instanceof \Magento\Framework\Model\AbstractModel) {
if($order->getState() == 'canceled' || $order->getState() == 'closed') {
//Your code here
}
}
return $this;
}
We can read more about observer : Magento 2 events list
NOTE: We should try with Plugin:
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="order_state_plugin"
type="Company\Module\Model\Plugin\OrderStatePlugin"/>
</type>
</config>
Plugin:
/**
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param \Magento\Sales\Api\Data\OrderInterface $result
* @return mixed
* @throws \Exception
*/
public function afterSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
$result
) {
if($result->getState() == Order::STATE_COMPLETE) {
......
}
return $result;
}
Best Answer
Here is how I would do it.
You need to create a module and a plugin for that.
In
app/code/Vendor/Module/etc/di.xml
you need the following:Then in your
app/code/Vendor/Module/Plugin/ListDispatchedEvents.php
file:Then I suggest you disable that module until you reach the page where you can click the Place Order button. Re enable module, click the button, disable the module once the the page has redirected you to the success page and check the
var/log/test.log
file.