Magento 2 get order after submission
In this tutorial I show you how to use event sales_order_save_after to get order after submission. In this event you can tasks that are needed to be done for each new order. For example to tell an external system to re-fetch orders from Magento.
Magento 2 get order after submission
First you need to create a new Magento 2 module as shown earlier. If you want to react on events, you need to create a file etc/events.xml in you module directory. There you can define each event to listen on and set a listener model.
1 2 3 4 5 6 | <?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="mymodule_sales_order_save_after" instance="MyCompany\MyModule\Observer\Observer" /> </event> </config> |
If you want to get informed about each new order, you need to use sales_order_save_after event, which is fired right after creation of a new order in database. This is also the last step in Magento 2 checkout before redirecting a customer to a checkout success page.
Event Listener
In our event listener model you only need to create a execute() method. If you need things like factories or repositories, you can inject them as usual in __construct() method. A simple example for my needs looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace MyCompany\MyModule\Observer; use Magento\Framework\Event\ObserverInterface; class Observer implements ObserverInterface { protected $connector; public function __construct() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); } public function execute(\Magento\Framework\Event\Observer $observer) { $order = $observer->getEvent()->getOrder(); $customerId = $order->getCustomerId(); if($customerId) #do something with order an customer } } |
I created a simple Model in module root directory called Observer/Observer.php. You can name it as you like both folder and model file. You only need to extend Magento 2 ObserverInterface class. There you need to implement execute() method, which gets an observer param as you may know from Magento 1. This observer model contains different information, based on used event. sales_order_save_after contans recently saved order object. You can use this order object to get or set information you need.
Conclusion
It is quite easy to create event listeners for Magento 2 events. This tutorial shows how to get order after submission.
Is the event is sales_order_save_after or sales_order_place_after little bit confused?
sales_order_save_after
Thank you very much. Your guide was really helpfull.
I’d like to add something, if anybody wants to use it only for new orders, they can simply add this:
$OrderStatus=$order->getStatus();
if ($OrderStatus == ‘pending’) {
//some work here
}
I’d like to ask you, how can i get customer information like phone etc, as well as the order’s products in this observer?
Thanks
You can get customer by customerId if you inject customerFactory for example