0

I made an observer for the catalog_product_save_after event.

I need to do some changes to the "brother" products.

To do this, I get the parent product id and then all his childens.

For each children (except the product where the event was triggered) I do my mods and then I call the save() method of \Magento\Catalog\Model\ProductFactory.

When the save() method is called, another event catalog_product_save_after is triggered, so I'm in a loop.

I want to observe the event when the form is submitted from the admin panel only, and not when I save the product programmatically.

What i thought i would do is to pass a parameter when I call the save() method, so next time the event is triggered, if my parameter exists I quit my observer.

I tried setting a session parameter like:

$this->_session->start();

if( $this->_session->getMyParam() !== null ){
    return false;
}

$this->_session->setMyParam('true');

...

$this->_session->unsMyParam();

but doesn't work. It seems the MyParam is always set, so my function return false every time.

Please any suggestion will be appreciated.

Thanks

1 Answer 1

1

You can use the "normal" php Session.

// SET PARAMETER VALUE

$_SESSION['PARAMETER_NAME'] = SOME OBJECT

// GET PARAMETER VALUE

 $value = isset($_SESSION['PARAMETER_NAME']) ? $_SESSION['PARAMETER_NAME'] : null;

I use this cache prices from an external webservices and it works perfectly.

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.