Hi fellow programmers,

I would like to know how I can hook in to execute code after a certain action in Magento 2 has been done. As we're running a custom warehouse system, I'd like to reserve a customer's products after an order has been confirmed, but I have no clue as to how to do that.

I've looked in to overriding certain functions using plugins, but that seems a bit redundant as I don't want to copy the entire function body just to execute my own code afterwards.

I've read something about the special "after", "before" plugin functions you can specify, but I don't know how to implement those.

Is there another way or can I use the "before" and "after" plugin functions? If so, can someone point me towards a good resource that explains just that?

Thanks in advance!

1 Answer 1


You can use observer to run your codes after each action. And in your case, you can use sales_order_save_after to perform your task. Let's say, if you want to perform codes after order status changed to Processing, we can write the following codes:

public function execute(\Magento\Framework\Event\Observer $observer)
    $order = $observer->getEvent()->getOrder();
    $status = $order->getStatus();
    if ($status == "processing"){
        //do your codes
    return $this;

I assume you know how to create an observer. If not, you can still refer to official documentation and refer to my codes above.

  • Thanks! I haven't implemented an observer yet ,but i'll find out how to do it!
    – Merijn
    Sep 13, 2017 at 7:48
  • If my method works, please accept my answer :)
    – PY Yick
    Sep 13, 2017 at 7:50
  • Will do, i'm still looking how to make sure the observer is registered, please give me some time to figure it out :P
    – Merijn
    Sep 13, 2017 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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