2

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?

  • What is your magento version? – Sohel Rana Apr 4 at 16:27
  • My magento version is 1.9.3.9 – Mastafis Apr 4 at 16:32
  • 1
    log file will not create because afterSave function never called! you need to call $this->afterSave(); after check isNew. – Pawan Apr 4 at 16:38
  • 1
    you need to pass $observer as parameter like $this->afterSave($observer); – Pawan Apr 4 at 16:44
  • 1
    you can also eliminate ` $this->isNew = true;` flag and direct call $this->afterSave($observer); in if condition! – Pawan Apr 4 at 16:48
1

Log file will not create because afterSave function never called! you need to call $this->afterSave($observer); after check isNew.

Like:

public function beforeSave($observer)
{
    $product = $observer->getEvent()->getProduct();
    if ($product->isObjectNew()) {
        $this->afterSave($observer);
    }
}

Note: you can also eliminate $this->isNew = true; flag and direct call $this->afterSave($observer); in if condition!

0

Try the following:

app/code/local/SR/MagentoCommunity/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <SR_MagentoCommunity>
            <version>0.0.0.2</version>
        </SR_MagentoCommunity>
    </modules>
    <global>
        <models>
            <sr_magentocommunity>
                <class>SR_MagentoCommunity_Model</class>
            </sr_magentocommunity>
        </models>
        <events>
            <catalog_product_save_before>
                <observers>
                    <sr_catalog_product_save_before>
                        <class>sr_magentocommunity/observer</class>
                        <method>catalogProductSaveBefore</method>
                        <type>singleton</type>
                    </sr_catalog_product_save_before>
                </observers>
            </catalog_product_save_before>
            <catalog_product_save_after>
                <observers>
                    <sr_catalog_product_save_after>
                        <class>sr_magentocommunity/observer</class>
                        <method>catalogProductSaveAfter</method>
                        <type>singleton</type>
                    </sr_catalog_product_save_after>
                </observers>
            </catalog_product_save_after>
        </events>
    </global>
</config>

app/code/local/SR/MagentoCommunity/Model/Observer.php

<?php
class SR_MagentoCommunity_Model_Observer
{
    protected $isNew = false;

    /**
     * @param Varien_Event_Observer $observer
     * @return $this
     */
    public function catalogProductSaveBefore(Varien_Event_Observer $observer)
    {
        /** @var Mage_Catalog_Model_Product $product */
        $product = $observer->getEvent()->getProduct();
        if ($product->isObjectNew()) {
            $this->isNew = true;
        }

        return $this;
    }

    /**
     * @param Varien_Event_Observer $observer
     * @return $this
     */
    public function catalogProductSaveAfter(Varien_Event_Observer $observer)
    {
        /** @var Mage_Catalog_Model_Product $product */
        $product = $observer->getEvent()->getProduct();
        if ($this->isNew) {
            $name = $product->getName();
            $sku = $product->getSku();
            $price = $product->getPrice();
            error_log("[{$name} ({$sku}) {$price}] aggiornato");
            Mage::log("[{$name} ({$sku}) {$price}] aggiornato", null, 'product-updates.log');
        }
        return $this;
    }
}

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.