3

I have a working event observer hooked to:

catalog_product_attribute_update_before

However I need the event that fires after the attributes have been saved. I had hoped that this would be:

catalog_product_attribute_update_after

but this is not firing. What event should I be listening for?

Furthermore if this event exists how should I get $product objects from the updated products. In my current class (listening for: catalog_product_attribute_update_before) I do the following:

public function execute( \Magento\Framework\Event\Observer $observer ) {

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    foreach ($observer->getData('product_ids') as $key => $prodID) :

        $objectManager->get('Magento\Catalog\Model\Product')->load( $prodID );

        $product = $this->getProduct( $prodID );

    endforeach;

}
2
  • you are looking for an event dispatched when the product attribute instance is saved or when a value of an attribute for a specific product is changed?
    – Marius
    Dec 16, 2015 at 15:11
  • I'm after an event fired after saving products that are edited in bulk on the catelog page of admin
    – GuyC
    Dec 16, 2015 at 16:00

1 Answer 1

11

There is the event catalog_product_attribute_update_before dispatched in Magento\Catalog\Model\Product\Action::updateAttributes().
There is no event for after the update.
but in Magento 2 every public method has 3 events by default. Well...they are not actually events, they are called plugins.
You can execute code before, after and instead of a certain method.
So you can execute some code around the method updateAttributes is executed.
You actually need to execute something after it ends, but you don't have access to the parameters when using the after interceptor. Something like this:

Create a file called di.xml in your module with this content:

<?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\Action">
        <plugin name="update-attributes" type="[Namespace]\[Module]\Model\Plugin\UpdateAttributes" />
    </type>
</config>

and this class: app/code/[Namespace]/[Module]/Model/Plugin/UpdateAttributes.php

<?php
namespace [Namespace]\[Module]\Model\Plugin;

use Magento\Catalog\Model\Product\Action\Interceptor;

class UpdateAttributes
{
    /**
     * @param Interceptor $interceptor
     * @param \Closure $closure
     * @param $productIds
     * @param $attrData
     * @param $storeId
     * @return Interceptor
     */
    public function aroundUpdateAttributes(
        Interceptor $interceptor,
        \Closure $closure,
        $productIds,
        $attrData,
        $storeId
    ) {
        //execute the original method and remember the result;
        $result = $closure($productIds, $attrData, $storeId);
        //do something with $productIds here
        return $result;
    }
}
1
  • This is a great answer!
    – scrowler
    Dec 16, 2015 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.