Question

I have set an event-observer in my custom module but observer is not getting called. i have enabled my log and i am getting this warning :

2013-08-14T11:17:48+00:00 ERR (3): Warning: include(Mage/CompanyName/ModuleName/Model/Observer.php): failed to open stream: No such file or directory  in /var/www/nyp/lib/Varien/Autoload.php on line 93
2013-08-14T11:17:48+00:00 ERR (3): Warning: include(): Failed opening 'Mage/CompanyName/ModuleName/Model/Observer.php' for inclusion (include_path='/var/www/nyp/app/code/local:/var/www/nyp/app/code/community:/var/www/nyp/app/code/core:/var/www/nyp/lib:.:/usr/share/php:/usr/share/pear')  in /var/www/nyp/lib/Varien/Autoload.php on line 93

Code in my config.xml to attach event and observer :

<global>
    <events>
        <sales_order_place_after>
            <observers>
                <CompanyName_ModuleName_Observer>
                    <type>singleton</type>
                    <class>CompanyName_ModuleName/observer</class>
                    <method>methodName</method>
                </CompanyName_ModuleName_Observer>
            </observers>
        </sales_order_place_after>
    </events>
 <global>

Code in my CompanyName/ModuleName/Model/Observer.php :

<?php

class CompanyName_ModuleName_Model_Observer
{
    public function __contruct()
        {

        }

    public function methodName(Varien_Event_Observer $observer)
    {

    }
}

Thanks,

Was it helpful?

Solution

Based on the class group you are using (CompanyName_ModuleName) you should use:

<global>
    <models>
        <CompanyName_ModuleName>CompanyName_ModuleName_Model</CompanyName_ModuleName>
    </models>
</global>

OTHER TIPS

It seems you have an error here:

<class>CompanyName_ModuleName/observer</class>

Everything prior to the slash is lowercased in using the model shorthand notation, and rarely should contain an underscore. This would be the shortname you provided when doing your setup of your module:

<global>
    <models>
        <yourmodel>CompanyName_ModuleName_Model</yourmodel>
    </models>
</global>

If you didn't define this, no worries. Use the fully-qualified path:

<class>CompanyName_ModuleName_Model_Observer</class>

I think this post should help you.

In your case it would be something like this:

<global>
    <events>
        <sales_order_place_after>
            <observers>
                <modulename_observername>
                    <type>singleton</type>
                    <class>modulename/observer</class>
                    <method>methodName</method>
                </modulename_observername>
            </observers>
        </sales_order_place_after>
    </events>
    <models>
        <modulename>
            <class>CompanyName_ModuleName_Model</class>
        </modulename>
    </models>
 <global>

cofig code would be like this.

<events>
          <core_block_abstract_prepare_layout_before>
               <observers>
                   <companyname_jquery_prepare_layout_before>
                       <class>modulename/observer</class>
                       <method>prepareLayoutBefore</method>
                   </companyname_jquery_prepare_layout_before>
               </observers>
           </core_block_abstract_prepare_layout_before>
</events>

this will sure help you to get better idea about how observer work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top