1

I would like to do some extra custom actions after a product price is changed. I was thinking to use the catalog_product_save_after event when a mass price update is done from the Product Grid -> Actions -> Update Attributes (via the bin/magento queue:consumers:start product_action_attribute.update). I've created the events.xml file in the etc folder with this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="catalog_product_save_after_queue"
                  instance="Vendor\Module\Observer\ProductSaveAfterQueue"/>
    </event>
</config>

This is my ProductSaveAfterQueue content:

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ProductSaveAfterQueue implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Here');
    }
}

Unfortunately, my log file is not created :( Do you know why and what kind of event or approach should I use instead ?

Thanks

4
  • Please move your events.xml file under path etc/adminhtml/. After that please check by insert die() in observer file
    – ZealousWeb
    Apr 26 at 10:09
  • Hi @ZealousWeb i moved it under the etc/adminhtml/ folder, I added the die() and also kept my log script. But nothing. The price is updated , seems like the observer is not fired. Not sure why. Apr 26 at 10:21
  • 1
    Please use this event "catalog_product_attribute_update_before"
    – ZealousWeb
    Apr 26 at 10:22
  • @ZealousWeb catalog_product_attribute_update_before works but only if I add the event in the etc folder not etc/adminthml. Is there an after event ? I've tried using catalog_product_attribute_update_after, but doesn't work. Weird Apr 26 at 10:32
0

Try using the event controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save the way the module Magento_cacheInvalidate is using to invalidate the caches (see vendor/magento/module-cache-invalidate/etc/events.xml for details).

4
  • Hi @Diana, thanks for your post . But I am using message queue , which runs via crons .The event: controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save seems to fired only from the controller. Apr 26 at 10:30
  • but why? you mentioned you wanted to track the price change from admin product grid. Apr 26 at 10:35
  • That change will add a new record in the queue_message table. The bin/magento queue:consumers:start product_action_attribute.update will do that actual update Apr 26 at 10:37
  • i see, but then you shouldn't care when the queue is processed, you should basically intervene at the point where the message is published to the queue #justsaying (and that is the action and implicitly the event mentioned above), unless i am missing something. Apr 26 at 10:46

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.