Magento 2.x trigger for product add/update/delete and import

In Magento 2.x, if you want to develop a module, in which product info used then in that case you should need for the trigger to get sync with the product info. Here are some of the trigger related to the product…follow the instruction below

1) Follow the path “app\code\vendorName\moduleName\etc\adminhtml\events.xml” and put the below code

[xml]/*
Product trigger @itechinsiders
*/
<event name="catalog_product_save_before">
<observer name="itech_product_save_before" instance="vendorName\moduleName\Observer\Products" />
</event>

<event name="catalog_product_save_after">
<observer name="itech_product_save_before" instance="vendorName\moduleName\Observer\Products" />
</event>

<event name="catalog_product_delete_before">
<observer name="itech_product_save_before" instance="vendorName\moduleName\Observer\Products" />
</event>

/*
Product Import trigger @itechinsiders
*/

<event name="catalog_product_import_bunch_save_after">
<observer name="itech_product_import_bunch_save_after" instance="vendorName\moduleName\Observer\ProductImport" />
</event>

<event name="catalog_product_import_bunch_delete_commit_before">
<observer name="itech_product_import_bunch_delete_commit_before" instance="vendorName\moduleName\Observer\ProductImport" />
</event>[/xml]

2) Now, let create two files “Products.php” & “ProductImport.php” inside “vendorName\moduleName\Observer\”

Products.php

[php]
<?php

namespace vendorName\moduleName\Observer;

use Magento\Framework\Event\Observer;

class Products implements \Magento\Framework\Event\ObserverInterface
{

public function execute(Observer $observer)
{
$product = $observer->getEvent()->getProduct();

$storeIds = $this->configHelper->getEnabledStoresForIndexing($product->getStoreId());
$this->logger->add($observer->getEvent()->getName());
foreach ($storeIds as $storeId) {
switch ($observer->getEvent()->getName()) {

case "catalog_product_save_before":
$this->catalogProductSaveBefore($product, $storeId);
break;
case "catalog_product_save_after":
$this->catalogProductSaveAfter($product, $storeId);
break;
case "catalog_product_delete_before":
$this->catalogProductDeleteBefore($product, $storeId);
break;

}
}
}

/*
* @itechInsider
* Product Trigger Before Save
* */

private function catalogProductSaveBefore($product,$storeId)
{
// Your Stuff Here

}

/*
* @itechInsider
* Product Trigger After Save
* */

private function catalogProductSaveAfter($product,$storeId)
{
// Your Stuff Here

}

/*
* @itechinsiders
* Product Trigger Delete Before
* */

private function catalogProductDeleteBefore($product,$storeId)
{
// Your Stuff Here
}

}
[/php]

ProductImport.php

 

[php]<?php
namespace vendorName\moduleName\Observer;

use Magento\Framework\Event\Observer;

class ProductImport implements \Magento\Framework\Event\ObserverInterface
{

public function execute(Observer $observer)
{
switch ($observer->getEvent()->getName()) {

case "catalog_product_import_bunch_save_after":
$productImport=$observer->getEvent()->getData(‘bunch’);
$this->catalogProductImportBunchSaveAfter($productImport);
break;

case "catalog_product_import_bunch_delete_commit_before":
$idsToDelete = $observer->getEvent()->getData(‘ids_to_delete’);
$this->catalogProductImportBunchDeleteCommitBefore($idsToDelete);
break;
}

}

/*
* @itechinsiders
* Product Trigger Import
*
* */

private function catalogProductImportBunchSaveAfter($productImport){

foreach($productImport as $importId){
$productId= $this->product->getIdBySku($importId[‘sku’]);
// do your stuff here
}
}

private function catalogProductImportBunchDeleteCommitBefore($idsToDelete)
{
// do your stuff Here
}

}
[/php]

So, by using these trigger you can sync the product data on any of the action.

Happy Coding 🙂

Related Post

Leave a Reply

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