cancel
Showing results for 
Search instead for 
Did you mean: 

magento 2 order status change event & sales_order_save_after observer

magento 2 order status change event & sales_order_save_after observer

Create a file events.xml in app/code/[Namspace]/[Module_Name]/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="sales_order_save_after">
        <observer name="sales_order_save_after" 
         instance="[Namspace]\[Module_Name]\Observer\OrderSaveAfter" />
    </event>
</config>

Create observer file OrderSaveAfter.php in app/code/[Namspace]/[Module_Name]\Observer\Sales\Order

<?php
namespace [Namspace]\[Module_Name]\Observer; 

use Magento\Framework\Event\ObserverInterface; 
use Psr\Log\LoggerInterface;
class OrderSaveAfter  implements ObserverInterface 
{

	protected $logger;
	public function __construct(LoggerInterface$logger
	)
	{
		$this->logger = $logger;
	}

	public function execute(\Magento\Framework\Event\Observer $observer) {
	    $order = $observer->getEvent()->getOrder();
	   $this->logger->critical('Order Status'.$order->getState());
	   
	}
}

Upgrade Magento setup

php bin/magento cache:flush

and deploy static content

php bin/magento setup:static-content:deploy -f