I'm trying to save the product in the observer of event "catalog_product_save_before", but somehow the admin panel is just loading its not saving product.
Can anyone please suggest the solution for this?
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_save_before">
<observer name="test_updateproductsku_observer_backend_catalog_productsavebefore_catalog_product_save_before" instance="Iflair\Updateproductname\Observer\Backend\Catalog\ProductSaveBefore"/>
</event>
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$_product = $observer->getProduct(); // you will get product object
$_sku = $_product->getSku(); // for sku
$_product->setSku("testNew1");
$_product->save();
}
Best Answer
You have observed the
"catalog_product_save_before"
event and again trying to save the product. So$_product->save()
code again observe your event and it runs your observer as a loop. If you want to change any attribute value you can just set the data and let it. It will automatically save your changes.So just remove the
$_product->save()
line and then try to save the product, it will work.