Back to Top

Observer for product when importing products in Magento 2

Updated 26 March 2024

Observer for the product when importing products in Magento 2

When a product is uploaded by importing then event catalog_product_save_after is not triggered.

Instead, the catalog_product_import_bunch_save_after event is dispatched in the Magento/CatalogImportExport/Model/Import/Product.php file.

So in the events.xml file, you will need to enter these lines of code

<event name="catalog_product_import_bunch_save_after">
    <observer name="webkul_module_catalog_product_import_bunch_save_after" instance="Webkul\Module\Observer\CatalogProductImportBunchSaveAfter" />
</event>

Next, create an observer file by the name CatalogProductImportBunchSaveAfter.php inside the folder Webkul/Module/Observer/ and write these lines of code to get uploaded product data

public function execute(\Magento\Framework\Event\Observer $observer)
{
    try {
        $bunch = $observer->getBunch();
        foreach($bunch as $product) {
            \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Psr\Log\LoggerInterface')
            ->info('Product Data '.json_encode($product));
        }
    } catch (\Execption $e) {
        \Magento\Framework\App\ObjectManager::getInstance()
        ->get('Psr\Log\LoggerInterface')
        ->info('Error '.$e->getMessage()); 
    }
}

In this way, you can get uploaded product data

Searching for an experienced
Magento 2 Company ?
Find out More

Happy coding 🙂

. . .

Leave a Comment

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


2 comments

  • Yoan
    • Mohd. Faizan (Moderator)
  • Back to Top

    Message Sent!

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

    Back to Home