Question

I want to send eamil notification to main admin, for modification avail in price by any admin.

Please help,

Thank You in advance.

Was it helpful?

Solution

You should use the around plugin for your requirement. around plugin should be used on afterSave($object) method of \Magento\Catalog\Model\Product\Attribute\Backend\Price class.

This class has been assigned as backend_model to product prices attributes so when product prices will be updated this backend_model class will be called.

So below given code will be useful to you to send the email when value of price attribute will be changed.

FILE: Kartik\PriceUpdate\etc\adminhtml\di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product\Attribute\Backend\Price">
        <plugin name="send_email_when_price_change" type="Kartik\PriceUpdate\Plugin\Product\Attribute\Backend\PricePlugin"/>
    </type>
</config>

FILE: Kartik\PriceUpdate\Plugin\Product\Attribute\Backend\PricePlugin.php

<?php
namespace Kartik\PriceUpdate\Plugin\Product\Attribute\Backend;

class PricePlugin
{
    const PRICE_ATTRIBUTE = 'price';

    /**
     * @param \Magento\Catalog\Model\Product\Attribute\Backend\Price $subject
     * @param callable $proceed
     * @param $object
     * @return mixed
     */
    public function aroundAfterSave(\Magento\Catalog\Model\Product\Attribute\Backend\Price $subject, callable $proceed, $object)
    {
        $result = $proceed($object);
        $attributeCode = $subject->getAttribute()->getAttributeCode();

        if ($attributeCode == self::PRICE_ATTRIBUTE) {
            if ($object->hasData($attributeCode)) {
                $originalData = $object->getOrigData($attributeCode);
                $newData = $object->getData($attributeCode);

                if ($originalData !== $newData) {
                    // implement your email logic here
                }
            }
        }
        return $result;
    }
}

Plugin is always better option while implementing this type of requirement. If plugin is not helpful then only you should move to different solution.

OTHER TIPS

You should be able to hook in to the catalog_product_save_after event to create an observer, & look at $observer->getProduct()->getData() to see the new data. You can compare a specific number of fields (eg: price, special_price) by checking getData() vs getOrigData()

There's a more complete answer here which should hold up for Magento 2.3.

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