0

In Magento 2, How to get order data(before and after save) using plugin function. I tried this solution but it is not working.

0

You can get Order data after save using following way:

/app/code/vendor/Module/etc/adminhtml/di.xml

<?xml version="1.0" ?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\ResourceModel\Order">
        <plugin name="after_place_order_save" type="Vendor\Module\Plugin\Order"/>
    </type>
</config>

/app/code/Vendor/Module/Plugin/Order.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Plugin;

class Order
{
    /**
     * [afterSave description]
     * @param  \Magento\Sales\Model\ResourceModel\Order $subject [description]
     * @param  [type]                                   $result  [description]
     * @param  [type]                                   $object  [description]
     * @return [type]                                            [description]
     */
    public function afterSave(
        \Magento\Sales\Model\ResourceModel\Order $subject,
        $result,
        $object
    ) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Order_data.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info(print_r($object->getIncrementId(), true));
        $logger->info(print_r($object->getId(), true));
        return $result;
    }
}

I hope it will helpful for you.

  • I need both data before and after saving in the same function using around plugin. – Saravanan DS Feb 14 at 6:25

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.