3

I try set new sku using catalog_product_save_after event, its continuously run this observer, But Not save the sku value.

events.xml:

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

Code:

    <?php

    namespace Vendor\Module\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
            $this->logger->addDebug($_sku);

             $_product->setSku("testNew1");
             $_product->save();
        }
    }

Suggest Me, Why Its Not saved Sku value in magento 2.

2 Answers 2

7

It's because you are using catalog_product_save_after. so on product save you are again saving and again this event called so bottleneck happen. I would suggest to use different event. or Use catalog_product_save_before event and just set the $_product->setSku("testNew1"); and not save $product object.

1
  • 1
    I have used catalog_product_save_before and save $_product->setSku("testNew1"); but SKU is not change after add product from back end side :( Commented Dec 19, 2018 at 9:48
1

I stuck in the same situation after spending lot of time when i remove/commented $_product->save(); it works for me.

So for the observer in Magento2 don't need to use $_product->save();

Thanks!!!

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.