Observer on mass update attributes of product in Magento 2
Whenever a product is saved in magento 2, catalog_product_save_after event is dispatched and we create an observer that will listen to any changes made to products.
But there is a time when the catalog_product_save_after event is not called when the 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 attribute data that was updated.
Hope this helps.
Happy coding 🙂
Be the first to comment.