SlateEntropy
SlateEntropy

Reputation: 4018

Magento Observer is not firing

I'm trying to fire an observer whenever a product is updated in the admin cp. To log the updates.

Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mod_Products>
            <version>1.0.0</version>
        </Mod_Products>
    </modules>

    <models>
        <Mod_Products>
            <class>Mod_Products_Model</class>
        </Mod_Products>
    </models>

    <events>
        <catalog_product_save_after>
            <observers>
                <Mod_Products_stock>
                    <type>singleton</type>
                    <class>Mod_Products_Model</class>
                    <method>logUpdate</method>
                </Mod_Products_stock>
            </observers>
        </catalog_product_save_after>
    </events>
</config>

Observer.php

class Mod_Products_Model_Observer {
    public function logUpdate($observer) {
        $event = $observer->getEvent()->getControllerAction()->getFullActionName();

        Mage::log('Event Fired: ' . $event);
        Mage::log(json_encode($observer->getEvent()));
    }
}

There are no errors and no output in the log.

Upvotes: 4

Views: 3420

Answers (7)

Shahroq
Shahroq

Reputation: 1039

I was struggling with the same situation and finally, found out the Mage_Log was not working. Try using something like die("I'm here"); instead of Mage::Log() for tracking and see whether it comes across your class or not. If so, try turning on Mage_Log via Configuration: Developer: Log Setting (Enabled=Yes). Also, check if Mage_Log is enabled at the Configuration: Advanced.

Upvotes: 0

Nmirach
Nmirach

Reputation: 1

check if your module exist in "disable module output" and enabled
System -> configuration -> advenced -> disable module output.
And check your system log file

Upvotes: -1

Shaista
Shaista

Reputation: 157

Once i got the same issue and i resolved that just by Flushing the system cache,so you can also try to flush the cache to refresh its configurations.

Upvotes: 0

Ronen Ness
Ronen Ness

Reputation: 10740

don't know why op accepted Jason's answer that didn't really solve it, but here's the correct answer for future seekers (I noticed it from Emi's comment on his own answer): missing adminhtml tag!

the config should look like this (assuming I didn't miss anything else):

<adminhtml>
    <events>
        <catalog_product_save_after>
            <observers>
                <Mod_Products_stock>
                    <type>singleton</type>
                    <class>Mod_Products_Model</class>
                    <method>logUpdate</method>
                </Mod_Products_stock>
            </observers>
        </catalog_product_save_after>
    </events>
</adminhtml>

hope this helps.

Upvotes: 2

justinpage
justinpage

Reputation: 653

A possible solution for those coming across this problem is that your Magento application may not have the proper permissions to write to the app/var folder. Especially if you are working with a UNIX/Linux based OS.

To solve this issue, simply locate your terminal to the root of your Magento application. Once you are there, use the following command to give your var folder the proper permissions:

sudo chmod 777 -R var/

Once I did this, my application was able to log and create other folders under the folder structure.

Upvotes: 0

Jason
Jason

Reputation: 1962

Let's go through the files one by one.

app/etc/modules/Mod_Products.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mod_Products>
            <!-- Change to Community codePool if in app/code/community -->
            <codePool>local</codePool> 
            <active>true</active>
        </Mod_Products>
    </modules>
</config>

app/code/local/Mod/Products/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mod_Products>
            <version>1.0.0</version>
        </Mod_Products>
    </modules>

    <!-- Marking models as global sometimes helps -->
    <global>
        <models>
            <Mod_Products>
                <class>Mod_Products_Model</class>
            </Mod_Products>
        </models>
    </global>

    <events>
        <catalog_product_save_after>
            <observers>
                <mod_products_model_observer>
                    <type>singleton</type>
                    <class>Mod_Products_Model_Observer</class>
                    <method>logUpdate</method>
                </mod_products_model_observer>
            </observers>
        </catalog_product_save_after>
    </events>
</config>

app/code/local/Mod/Products/Model/Observer.php

class Mod_Products_Model_Observer {
    public function logUpdate($observer) {
        $event = $observer->getEvent()->getControllerAction()->getFullActionName();

        Mage::log('Event Fired: ' . $event);
        Mage::log(json_encode($observer->getEvent()));
    }
}

Pay attention to file names and directories, and to the different xml handles in my updated config.xml. Let me know if this works for you.

Upvotes: 0

Emi
Emi

Reputation: 1018

The class inside your config must be the Observer class, so change to:

<class>Mod_Products_Model_Observer</class>

Upvotes: 0

Related Questions