I want to run the observer only when the product is created and not when it is updated. I found this discussion: product-creation-and-saving-existing-products and I followed Tim Ramsey's advice.
So I put this in the config.xml
<global>
<models>
<wally_logproductupdate>
<class>Wally_LogProductUpdate_Model</class>
</wally_logproductupdate>
</models>
<events>
<catalog_product_save_before>
<observers>
<wally_logproductupdate>
<class>wally_logproductupdate/observer</class>
<method>beforeSave</method>
<type>singleton</type>
</wally_logproductupdate>
</observers>
</catalog_product_save_before>
</events>
</global>
and this in Observer.php:
class Wally_LogProductUpdate_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) {
$name = $product->getName();
$sku = $product->getSku();
$price = $product->getPrice();
Mage::log("[{$name} ({$sku}) {$price}] aggiornato", null, 'product-updates.log');
}
}
}
The problem is that the log file is not updated when a new product is created.
How can i solve?
afterSave
function never called! you need to call$this->afterSave();
after checkisNew
.$observer
as parameter like$this->afterSave($observer);
$this->afterSave($observer);
in if condition!