0

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?

asked Jul 14, 2016 at 13:59

1 Answer 1

1

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!

answered Jul 14, 2016 at 14:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.