1
votes

I'm trying to fire an observer when a product is added to the shopping cart, here is my code:

app/etc/modules/Mydons_Eventdemo.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mydons_Eventdemo>
            <active>true</active>
            <codePool>local</codePool>
        </Mydons_Eventdemo>
    </modules>
</config>

app/code/local/Mydons/Eventdemo/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mydons_Eventdemo>
            <version>0.1.0</version>
        </Mydons_Eventdemo>
    </modules>
    <frontend>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Mydons_Eventdemo_Model_Observer>
                        <type>singleton</type>
                        <class>Mydons_Eventdemo_Model_Observer</class>
                        <method>Mytestmethod</method>
                    </Mydons_Eventdemo_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </frontend>
</config>

and app/code/local/Mydons/Eventdemo/Model/Observer.php

<?php

class Mydons_Eventdemo_Model_Observer {
    public function __construct()
    {
        echo 'hello world';
        die();
    }
    public function Mytestmethod($observer) {

        $event = $observer->getEvent();  //Fetches the current event
        $product = $event->getProduct();
        $eventmsg = "Current Event Triggered : <I>" . $event->getName() . "</I><br/> Currently Added Product : <I> " . $product->getName()."</I>";
        //Adds Custom message to shopping cart
        echo Mage::getSingleton('checkout/session')->addSuccess($eventmsg);
        //Your Custom Logic Here
        //you can use print_r($product) here to get more details
    }

}

But when I add a product to the shopping cart, it just ran like it usually does. Is there anything wrong here? Please help!

Update: I checked the log file: system.log and got these:

2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 22: parser error : Premature end of data in tag config line 21  in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352
2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]:   in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352
2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: ^  in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352
2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 22: parser error : Premature end of data in tag config line 2  in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352
2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]:   in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352
2014-05-27T07:17:19+00:00 ERR (3): Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: ^  in D:\Webserver\htdocs\magento\includes\src\__default.php on line 22352

But I have no idea what it is.

Update2: I run it again and get a more clearly log message:

2014-05-27T07:29:00+00:00 ERR (3): Warning: include(D:\Webserver\htdocs\magento\includes\src\Mydons_Eventdemo_Model_Observer.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory  in D:\Webserver\htdocs\magento\includes\src\Varien_Autoload.php on line 93
2014-05-27T07:29:00+00:00 ERR (3): Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'D:\Webserver\htdocs\magento\includes\src\Mydons_Eventdemo_Model_Observer.php' for inclusion (include_path='D:\Webserver\htdocs\magento\includes\src;.;D:\Webserver\php\PEAR')  in D:\Webserver\htdocs\magento\includes\src\Varien_Autoload.php on line 93
3
Are your paths to the files correct app/code/Mydons/Eventdemo/etc/config.xml is incorrect it should be app/code/local/Mydons/Eventdemo/etc/config.xml - Zefiryn
I'm sorry it was a typo error, the path is app/code/local/Mydons/Eventdemo/etc/config.xml, I editted my question. - user1985916
Try compiling the code again. and the flush cache and check - Dushyant Joshi
Still the same result and log message, and cache is not enabled in my magento. - user1985916
See the same is mentioned here. stackoverflow.com/questions/11611433/… - Dushyant Joshi

3 Answers

1
votes

As you described in question "the include warning" , it seems like you have the compiler enabled but it cannot find the proper file i.e. Mydons_Eventdemo_Model_Observer.php.

Try recompiling the code again. Flush the cache, if enabled, then check if it is working fine

2
votes

You have die() in constructor so when magento creates the class the script is stopped and the method is not called at all.

0
votes

Try with below one

<config>
    <modules>
        <Mydons_Eventdemo>
            <version>0.1.0</version>
        </Mydons_Eventdemo>
    </modules>
    <global>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Mydons_Eventdemo_Model_Observer>
                        <type>singleton</type>
                        <class>Mydons_Eventdemo_Model_Observer</class>
                        <method>Mytestmethod</method>
                    </Mydons_Eventdemo_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </global>
<config>