2

I have truble with observer, don't know how to solve it!

Here is part of the config.xml and Observer.php files

Mycompany/Mymodule/etc/config.xml

<?xml version="1.0"?>    
<global>
        <models>
            <mymodule>
                <class>Mycompany_Mymodule_Model</class>
                <resourceModel>mymodule_mysql4</resourceModel>
            </mymodule>
            <mymodule_mysql4>
                <class>Mycompany_Mymodule_Model_Mysql4</class>
                <entities>
                    <mymodule>
                        <table>mymodule</table>
                    </mymodule>
                </entities>
            </mymodule_mysql4>
        </models>
        <events>
            <checkout_type_onepage_save_order_after>
                <observers>
                    <mycompany_mymodule_order_complete>
                        <class>mymodule/observer</class>
                        <method>mymoduleCompleteOrder</method>
                    </mycompany_mymodule_order_complete>
                </observers>
            </checkout_type_onepage_save_order_after>
       </events>
 </global>

Mycompany/Mymodule/Model/Observer.php

class Mycompany_Mymodule_Model_Observer 
{
    protected $_config;

    protected function _construct()
    {
        $this->_config = Mage::getModel('mymodule/config');
    }

    public function mymoduleCompleteOrder($observer)
    {...
      ....
    } 
7
  • Is the rest of your module working? Do you have the module declaration file in app/etc/modules ?
    – Marius
    Apr 7 '14 at 20:37
  • yes, working fine, only observer do not work!
    – sIiiS
    Apr 7 '14 at 20:54
  • IN the config.xml you declared your method used for the event mymoduleCompleteOrder and in the observer the method name is mymodulesCompleteOrder. There is an additional s in the observer class. Could that be it?
    – Marius
    Apr 7 '14 at 20:57
  • it my fault in write here! both same!
    – sIiiS
    Apr 7 '14 at 21:07
  • try Mage::getModel('mymodule/observer'); if this works try $observer-> mymoduleCompleteOrder('test'); if this works too, you need to check, whether the event is fired!? Apr 7 '14 at 21:21
4

I do not know what exactly problem you have, but I see one problem in your code.

In your observer you have _construct() method.

It is not the php constructor (php constructor has two underscores).

Also your observer does not extend Mage_Core_Model_Abstract which runs _construct()` method during object initialization.

So as a result _construct() will not be executed and protected $_config; will always be null.

If you use $this->_config in mymoduleCompleteOrder(), it can generate error, and your code is not executed correctly.

1
  • Yes true, I removed protected $_config; and _construct() from my code and then worked fine
    – sIiiS
    Apr 8 '14 at 20:40

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.