Question

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

Was it helpful?

Solution 3

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');
    }

OTHER TIPS

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;
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top