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?

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!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.