I want to catch the moment of creating order – both in frontend by client and in administration panel by user. I've got a plugin for it:
class Plugin
{
public function afterSave(
$subject,
$order, $additional = []
)
{
exit('aftersave');
}
public function afterPlace(
$subject,
$order, $additional = []
)
{
$order_id = $order->getEntityId(); // this is null
exit('afterPlace');
}
}
The afterSave method is not executed when creating order. The second – afterPlace method is executed, but I cat't get the entity id.
How to do it? What method should I listen to or how to get the order id?
Best Answer
I suggest you use a observer.
You can observe the following events
sales_order_place_after
sales_order_place_before
You can use this Magento 2 Module Creator to create example code.
Fill in the observer form an generate code.
With in the observer class method you can get the order like this
$order = $observer->getOrder();
$order->setSomething('test');
$order->save(); // Dont use this if you listen to a save event!!
Succes!