I am trying to add product programmatically on catalog_product_save_after observer.

Basically what i try to achieve is create a Duplicate Sample product of current product if PRODUCT-SKU-sample does not exist otherwise Update the price of PRODUCT-SKU-sample Sample product.


Can i use Plugin or interceptor instead of using Observer to create Sample Product on Save Product Event ?

File : Namespace/Modulename/Observer/Catalog/ProductSaveAfter.php

$_product = $observer->getProduct();
$isSampleOrderEnabled = $observer->getProduct()->getData('enable_sample_order');

if($isSampleOrderEnabled == 1){

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
    $_mediapath = BP.'/pub/media/'. 'catalog/product' . $_product->getImage();
    $imageUrl  =  $store->getBaseDir(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product' . $_product->getImage();

    $getProduct = $objectManager->get('Magento\Catalog\Model\Product');
    $sampleSkuExists = $getProduct->getIdBySku($_product->getSku().'-sample');

    $sampleProductPrice = $observer->getProduct()->getData('sample_price');
    if($sampleSkuExists == FALSE) {

        $product = $objectManager->create('Magento\Catalog\Model\Product');
        try {
            $product->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                    ->setAttributeSetId($_product->getAttributeSetId()) //ID of a attribute set named 'default'
                    ->setSku($_product->getSku().'-sample') //SKU
                    ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
                           'use_config_manage_stock' => 0, //'Use config settings' checkbox
                           'manage_stock'=>1, //manage stock
                           'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
                           'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
                           'is_in_stock' => 1, //Stock Availability
                           'qty' => 99999//qty

            $res = $product->save();
            echo "AFTER SAVING THE NEW PRODUCT";

            echo $product->getEntityId();
            // Save the new Sample Product ID to original product Attribute. 
        } catch (Exception $e ) {
            echo    $e->getMessage();
    } else {
        echo "UPDATE CODE GOES HERE";

I cant get past $product->save();. Code execution stop right there without any notice/error.

Checked all the log files. there is no relevent information there as well.


It is logical an issue in your code.

you have another save()[ $res = $product->save();] function inside call catalog_product_save_after observer.

it make recursive call of your observer. because of save() call catalog_product_save_after which fire again your observer.

That make recursive call of your observer.

So, you should avoid it by some logic

  • Well it looks like this is the issue. Can you please help me use Plugin or Interceptor (DI) after product save event. May 31 '17 at 7:16

Solved this by adding conditional logic.
Set the Session variable for session while executing Observer.

$_SESSION['NameSpace']['ModuleName'] = 'Y'; 

And then checked the very first thing in function if session variable exist. And return if value is set

if(isset($_SESSION['NameSpace']['ModuleName']) && $_SESSION['ModuleName']['ModuleName'] == 'Y' ){
    return '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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