3

I can't get an observer working, I'm using a fresh Magento install of 1.9.10.

My namespace: TM

My module: ProductConditions

app/etc/modules/TM_ProductConditions.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <TM_ProductConditions>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </TM_ProductConditions>
    </modules>
</config>

app/code/local/TM/ProductConditions/etc/config.xml:

<global>
    <events>                
        <catalog_product_save_before>
            <observers>
                <ProductConditions>
                    <type>singleton</type>
                    <class>TM_ProductConditions_Model</class>
                    <method>catalog_product_save_before</method>
                </ProductConditions>
            </observers>
        </catalog_product_save_before>
    </events>
</global>

app/code/local/TM/ProductConditions/Model/Observer.php:

<?php
class TM_ProductConditions_Model_Observer
{
    public function catalog_product_save_before($observer)
    {
        file_put_contents("C:\\mage test.txt", "test");
        //$product = $observer->getProduct();
    }
}
Mex
  • 386
  • 1
  • 6
  • 16
  • Try 2 things. 1: change your fn() name to test($observer) and then do a die(var_dump(test)) instead of the file_put_content. Try savinf a product and see if you get to the var_dump. – mbalparda Feb 10 '15 at 14:54
  • I renamed my observer's method to test and stuck a var_dump in there, I did not see the output in my browser. The AJAX request redirects the browser as normal after saving a new product. – Mex Feb 10 '15 at 15:10
  • Solved, forgot to disable the caches. – Mex Feb 10 '15 at 15:59

2 Answers2

3

This is your problem:

<class>TM_ProductConditions_Model</class>

it should be

<class>TM_ProductConditions_Model_Observer</class>

[EDIT] Your observer is not picked up because the config file is wrong.
it should look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <TM_ProductConditions>
            <version>1.0.0</version>
        </TM_ProductConditions>
    </modules>
    <global>
        <events>                
            <catalog_product_save_before>
                <observers>
                    <ProductConditions>
                        <type>singleton</type>
                        <class>TM_ProductConditions_Model_Observer</class>
                        <method>catalog_product_save_before</method>
                    </ProductConditions>
                </observers>
            </catalog_product_save_before>
        </events>
    </global>
</config>   
Marius
  • 197,939
  • 53
  • 422
  • 830
  • Thanks but unfortunately it didn't fix it. I will keep the _Observer at the end now. – Mex Feb 10 '15 at 15:06
  • one more thing...config.xml contains only what you posted or everything is wrapped up in a <config> tag? – Marius Feb 10 '15 at 15:09
  • config.xml only contains what I posted. – Mex Feb 10 '15 at 15:11
  • @Mex. IN that case, see my update. – Marius Feb 10 '15 at 15:20
  • Thanks again, I'm now using the config.xml you gave me, but it's still not working! :( – Mex Feb 10 '15 at 15:29
  • Found the problem, I had forgotten to disable the caches after a fresh install of Magento. Thanks for all your help. – Mex Feb 10 '15 at 15:58
  • @Marius I just confused, which one is correct in the observer : tm_productconditions/observer or TM_ProductConditions_Model_Observer – Gem Jun 28 '19 at 07:30
3

You don't have a complete class reference here:

<class>TM_ProductConditions_Model</class>

It should be a full class name:

<class>TM_ProductConditions_Model_Observer</class>

You can also use the abstract factory in this node. Setup models in your config:

<config>
    <global>
        <models>
            <tm_productconditions>
                <class>Tm_ProductConditions_Model</class>
            </tm_productconditions>
        <models>
    </global>
</config>

Now you can place this in your observer:

<class>tm_productconditions/observer</class>
Ryan Street
  • 2,020
  • 14
  • 12