1

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();
}
2
  • could you plz add code you have tried ? did you checked var/log ?
    – Pawan
    Commented Aug 16, 2021 at 10:21
  • it is keep loading, not adding anything to log
    – Yogesh
    Commented Aug 16, 2021 at 10:24

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.