How to Get Order Data from “sales_order_place_after” Event in Magento 2?

How to Get Order Data from “sales_order_place_after” Event in Magento 2?

Magento 2 is a leading eCommerce platform as shown in the below statistic.

leading-ecommerce-platform

Source: Statista

Together with WooCommerce and Shopify, the three are clearly the preferred choices for online merchants. Speaking of Magento, it offers a wide range of features that streamlines the store’s management, especially related to order management. As the title indicates, we will focus on a very specific feature - sales_order_place_after event in Magento 2.

For those of you running a business, you’ll know that a store’s work does not end even after a customer completes an order. Instead, the real work begins once the order is placed. The merchant must provide the buyer with a confirmation, share their address with the shipment company, and then follow up with the customer to ensure there were no issues.

In short, it is a hectic process. But where does the Magento 2 sales_order_place_after feature come into play? As mentioned just now, the store’s work does not end once the order is placed. It needs to implement post-purchase processes such as pushing order data to a third-party web service, calculate tax based on order attributes, or forward the information to the shipping company.

This can only be done after the admin retrieves the order status or complete order information. Order data is retrieved using Magento Events and Observers which are central to extending Magento functionality. Magento also allows creating custom events in addition to its own events. Events are dispatched when certain actions are triggered and pass data to the Observer.

Steps To Get Order Data From “Sales_order_place_after” Event

Step 1: Create event.xml file at app/code/Vendor/Extension/etc/

 
<?xml version="1.0"?>
<configxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<eventname="sales_order_place_after">
<observername="place_order_after" instance="Vendor\Extension\Observer\OrderdataObserver" />
</event>
</config>

Step 2: Create the Observer file OrderdataObserver at app/code/Vendor/Extension/Observer/

 
<?php 
namespace Vendor\Extension\Observer; 
use Magento\Framework\Event\ObserverInterface; 
use Psr\Log\LoggerInterface; 
class OrdedataObserver implements ObserverInterface 
{ 
protected $logger; 
public function __construct(LoggerInterface$logger) 
{ 
$this->logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
 try 
{
    $order = $observer->getEvent()->getOrder();
}
catch (\Exception $e) 
{
 $this->logger->info($e->getMessage());
}
  }
}

Conclusion

This concludes our article. You can modify the code to get the order id or any other particular order information. If you are facing any issue with getting Order Data from “sales_order_place_after” Event in Magento 2, don’t hesitate to contact us.

Related Articles: