Reading list Switch to dark mode

    Observer on mass update attributes of product in Magento 2

    Updated 26 March 2024

    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.

    update_att_paul_blog

    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-

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    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 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home