Thursday, 14 February 2019

Get product from save event observer - Magento 2

Get product from save event observer - Magento 2

If you want to $productobj after saving product from backend then you can easily use catalog_product_save_after event.
I am assuming that you already know how to create a module in M2.
Put this events.xml in below path
app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path
app\code\YOUR_NAMESPACE\YOURMODULE\Observer\


<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{   
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $_sku=$_product->getSku(); // for sku

    }  
}

No comments:

Post a Comment