Currently I have a plugin which has the following in the di.xml

<type name="Magento\Sales\Api\OrderRepositoryInterface">
    <plugin name="MyModule_OrderAfterSave" type="Company\MyModule\Plugin\OrderAfterSave" sortOrder="10" disabled="false" />

This enables me to call my custom class when an order is saved. However what I need to do is call my plugin any time an order is updated.

The reason for the question is that I have a payment gateway extension installed and it saves the order before taking payment and then updates the order based upon if payment was successful. So I need to call my plugin after the payment has been success or failed

  • Refer this link, it will help you. – Pratik Oza Jan 12 '18 at 17:54

If you want to call your class only when order payment is succeed or fail then it is not good to use your class on order save. If you will use this method then your class will always (i.e invoice save, shipment save, credit memo and many order save which you can do with order from back-end).

If you want to call your class on payment succeed or fail then you can use event sales_order_place_after event , this event will fire right after your payment.

As you can see \Magento\Sales\Model\Order class

public function place()
        $this->_eventManager->dispatch('sales_order_place_before', ['order' => $this]);
        $this->_placePayment(); // this is the place where magento does the payment operation
        $this->_eventManager->dispatch('sales_order_place_after', ['order' => $this]);
        return $this;

In this event you can get $order object like $order = $observer->getEvent()->getOrder();

Note: If you are using some 3rd party payment module then you need to confirm with this event because I had some experience with that this event is not executing only some 3rd party module

  • Thanks for the suggested solution however this will mean using events and observers which I was led to believe this is the old way (Magento 1) of doing it and I should be using plugins, the new way? – Goose84 Jan 14 '18 at 14:36
  • Why you want to achieve this by plugin ? If you can achieve simply by just event. Look plugin is used whenever you don't any option to change then you can use before or around or after . @Goose84 – Keyur Shah Jan 14 '18 at 18:40

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.