How to Get Order Data From Magento 2 “sales_order_place_after” Event
Admin would want to use order data after an order is placed for many purposes. Admin may want the order data to send to shipping carriers for order delivery or send an SMS to the customer for acknowledging their order or calculate taxes based on product, location, and other such factors. To retrieve the order data, one needs to use Events and Observers in Magento 2.
Events in Magento 2 are dispatched based on an action performed and it passes data to the observer. Observers are Magento classes that are executed when an event is dispatched. Here, we will make use of this concept to get order data from Magento 2 “sales_order_place_after” event.
Method to Get Order Data From Magento 2 “sales_order_place_after” Event
Step 1: Create events.xml file at app/code/Vendor/Extension/etc/
1 2 3 4 5 6 7 |
<?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_place_after"> <observer name="place_order_after" instance="Vendor\Extension\Observer\Orderplaceafter"/> </event> </config> |
Step 2: Create Orderplaceafter.php at app/code/Vendor/Extension/Observer/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; use Psr\Log\LoggerInterface; class Orderplaceafter 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()); } } } |
Follow these steps and get order data from Magento 2 “sales_order_place_after” event to use it either for better administration or enhance the customer experience!
For a similar solution in Magento 1, check Get Order Data From Magento “sales_order_place_after” Event.
Doubts will be entertained in the comments section below. Also, let me know how have you used this order data for the betterment.
Rate the post with 5 stars if you like it!
Happy Coding 🙂
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
22 Comments
hey my event is not invoking i tried using sales_order_place_before,sales_order_place_after ,checkout_submit_all_after,checkout_onepage_controller_success_redirect,checkout_onepage_controller_success_action events in my custom module on etc/frontend/events.xml but only checkout_onepage_controller_success_action event is working but at that point my order has already been placed , can you please tell why this is happening , i am checking on both guest user as well as login user , or is the the problem of frontend/events.xml?????
Hey Ankush,
All this events are invoking after order place, so you need to get order related Data.
Hi Sanjay,
It’s not working for me, I use the Magento Order APIs, for getting the complete order data, but after all OrderAPIs run, when I check the log, it return blank JSON. Magento V2.4.0. Could you please help?
Thanks!
Hello Alok,
The same is working from our end.
you also can try by adding events.xml in webapi_rest directory.
Thank You
Hi,
I want to get the JSON data similar to get order API using the webhook.
Hello Sudhakar,
Please refer the rest API reference:
https://magento.stackexchange.com/questions/154198/magento-2-how-to-get-order-information-using-rest-api
Thank You
Funcionou! Obrigado, você sabe me dizer qual evento é disparado quando e atualizado o pedido?
Hello Handersen Adriano Damasceno Monteiro,
You can use sales_order_save_after event
Thank You
It’s not working for me. I put die to check but it’s not triggering at all.
Hello Sarvesh,
It’s properly working on our end.
Please recheck your implemented code.
Thank You.
I want to pass order details to my CRM after order successful placed. Is this work for me?
Hello Jay,
Yes, you can use the above method where you’ll get all the order details.
Thanks.
hi, How can I see the result?
You will get object of the order in the $order variable.
You have explain it very good. but unfortunately, We will not get entity_id of order and order_item in this observer.
for that, I think we need to use plugin for the same like as
https://magento.stackexchange.com/questions/208278/how-to-get-order-id-using-sales-order-place-after-event?answertab=votes#tab-top
Do you have any other solution for this?
Hi Anil,
To get entity_id you need to use $order->getId();
how to get output for the above code
As such you won’t see any output, all you get is the order data which can later be used in different ways. Are you expecting any other output? If yes, please mention what.
The $order variable has the order data of last-placed order.
I cannot find the Sales folder in app/code/local/ and I have no idea why. Could you please help?
You need to develop the Sales/Registration extension and then place the code given in the solution.
Can I use this on Magento 1.9.3.8?
No, the above solution is specifically for Magento 2.
However, you may refer here for the issue.