8

I have observer targeted on "catalog_product_save_after" event.

Need to check if current product is completelly new and not one of existing items.

How to do this?

2
  • You want to check the current product is existing in quote or not? Commented Jan 20, 2017 at 15:03
  • Not sure about quote, because haven't investigated mechanism of product creation. To be more concrete: need to find out if admin creates new product. For existing products my action shouldn't perform.
    – white-imp
    Commented Jan 20, 2017 at 15:09

2 Answers 2

8

For detecting new entity you can use

$product = $observer->getEvent()->getProduct();
$product->isObjectNew();

Source: \Magento\NewRelicReporting\Model\Observer\ReportProductSaved

0
4

For me the isObjectNew() was available in catalog_product_save_before, but was not available in catalog_product_save_after. I simply created my own isNew variable in the Observer and set it to true in beforeSave and checked it in the afterSave methods.

<?php
class Some_Module_Model_Observer{
protected $isNew = false;
public function beforeSave($observer) {
    $product = $observer->getEvent()->getProduct();
    if($product->isObjectNew()){
        $this->isNew = true;
    }
}
public function afterSave($observer) {
    $product = $observer->getEvent()->getProduct();
    if($this->isNew){
        /*Add Logic Here*/
    }
}

}

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.