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?
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?
For detecting new entity you can use
$product = $observer->getEvent()->getProduct();
$product->isObjectNew();
Source:
\Magento\NewRelicReporting\Model\Observer\ReportProductSaved
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*/
}
}
}