0

In Magento 2.3, How to get order statuses, before and after change using the plugin?

1

You can use the plugin on Class Magento\Sales\Model\ResourceModel\Order.

Create plugin on Magento\Sales\Model\ResourceModel\Order::save()

Here, I have used after the plugin.

See more details about After plugin Check https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html#after-methods

Code

<?php 
namespace StackExchanges\Sales\Plugin\Magento\Sales\Model\ResourceModel;

class Order
{

    public function afterSave(
        \Magento\Sales\Model\ResourceModel\Order $subject,
        $result,
        $object
    ) {
        $oldData = $object;
        $oldStatus = $object->getStatus();
        $newData = $result;
        $newsStatus = $newData->getStatus();
        return $result;
    }
}
| improve this answer | |
  • Thanks @AmitBera. I will check it. – Saravanan DS Feb 7 at 12:49
  • It is afterSave or aroundSave? – Saravanan DS Feb 10 at 5:45
  • It is Aftersave .,.. – Amit Bera Feb 10 at 5:47
  • 1
    I tried the solution, I am getting this error: <b>Fatal error</b>: Uncaught Error: Call to undefined method Magento\Sales\Model\ResourceModel\Order\Interceptor::getStatus() in root\app\code\Namesapace\Module\Plugin\OrderStatePlugin.php:31 – Saravanan DS Feb 10 at 7:19
  • It is not working. – Saravanan DS Feb 11 at 11:24
1

Use beforeSave plugin for order model, with below two methods

old value: $order->getOrigData('status');

new value: $order->getData('status');

Let me know if this helps.

| improve this answer | |
  • Hi @YashPatadia, What will be the plugin class? – Saravanan DS Feb 14 at 7:34
  • Magento\Sales\Model\Order – Yash Patadia Feb 14 at 7:36
  • Okay I will check and let you know. – Saravanan DS Feb 14 at 7:38
  • Yes it is worked. But used \Magento\Sales\Model\ResourceModel\Order plugin class instead Magento\Sales\Model\Order. – Saravanan DS Feb 14 at 8:19
  • Great :) ...... – Yash Patadia Feb 14 at 8:45
1

Finally I fixed this issue.

  1. In di.xml:
<type name="Magento\Sales\Model\ResourceModel\Order">
     <plugin name="order_state_plugin"
         type="Vendor\YourModule\Plugin\NameForYourPlugin"/>
</type>
  1. In NameForYourPlugin File:
    public function afterSave(
        \Magento\Sales\Model\ResourceModel\Order $subject,
         $result,$object
    ){
        $oldData = $object->getOrigData('status');
        $newData = $object->getData('status');
    }
| improve this answer | |

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.