Request Quote

Observer on mass update attributes of product in Magento 2

Observer on mass update attributes of product in Magento 2

Whenever a product is saved, catalog_product_save_after event is dispatched and we create observer that will listen to any changes made in products. But there is a time when catalog_product_save_after event is not called when product is saved. This happens when mass action Update Attributes is applied to products.

So, what you can do is use event catalog_product_attribute_update_before in this case.

In file /vendor/magento/module-catalog/Model/Product/Action.php this event has been dispatched before updating the products

$this->_eventManager->dispatch(
    'catalog_product_attribute_update_before',
    ['attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId]
);

So in your observer you can get these parameters like this-

public function execute(\Magento\Framework\Event\Observer $observer)
{
    try {
        $productIds = $observer->getProductIds();
        $attributes = $observer->getAttributesData();   
    } catch (\Execption $e) {
        $this->logger->info('Error : '.$e->getMessage());
    }
}

In this way you can get the product ids and the attributes data that was updated.
Hope this helps.
Happy coding 🙂

. . .

Comment

Add Your Comment

Be the first to comment.

css.php