0

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

3 Answers 3

2

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');
    }
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;
    }
}
9
  • Thanks @AmitBera. I will check it. Feb 7, 2020 at 12:49
  • It is afterSave or aroundSave? Feb 10, 2020 at 5:45
  • It is Aftersave .,..
    – Amit Bera
    Feb 10, 2020 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 Feb 10, 2020 at 7:19
  • It is not working. Feb 11, 2020 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.

5
  • Hi @YashPatadia, What will be the plugin class? Feb 14, 2020 at 7:34
  • Magento\Sales\Model\Order Feb 14, 2020 at 7:36
  • Okay I will check and let you know. Feb 14, 2020 at 7:38
  • Yes it is worked. But used \Magento\Sales\Model\ResourceModel\Order plugin class instead Magento\Sales\Model\Order. Feb 14, 2020 at 8:19
  • Great :) ...... Feb 14, 2020 at 8:45

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.