0
votes

I am trying to create an event dispatcher in Magento (which should be called after a product gets added to cart).

What I've done so far:

I Googled this and found this link. I followed the instructions specified in this link. I configured the config.xml file and created a Observer.php filder under mymodule/model folder. But, I can't see any result from this.

(The code for config.xml and Observer.php file that i used in my example can be found under http://goo.gl/O7dBy, my custom module name is Crossdata and package name is MyModule - am i doing it wrong?)

Any other link with simple helloworld event dispatcher example would be helpful.

Thanks, Balan

3
The reason your code is not showing on SO might be that you're using unusual whitespaces (NO-BREAK SPACEs, 0xC2 0xa0) all over your code. Try to reduce to normal spaces and tabs and it should show up.Jürgen Thelen
will breaks and spaces affect the code?balanv
For syntax coloring I could imagine it will. Functional? I don't think so, but never checked that as I never user other whitespace than space and tab to format.Jürgen Thelen

3 Answers

1
votes

Looking at your code downloaded from http://goo.gl/O7dBy you define:

<events>
    <add_to_cart_after>
        <observers>
            <add_to_cart_after>
                <class>dispatcher/observer</class>
                <method>hookToAddToCartAfter</method>
            </add_to_cart_after>
        </observers>
    </add_to_cart_after>
</events>

and define the model alias of dispatcher as:

<models>
    <dispatcher>
        <class>MyPackage_Crossdata_Dispatcher_Model</class>
    </dispatcher>
</models>

The resulting class name would be MyPackage_Crossdata_Model_Dispatcher_Model_Observer, but your observer class is named MyPackage_Crossdata_Dispatcher_Model_Observer, missing the Model in between Crossdata and Dispatcher.

I'd recommend to drop the whole dispatcher models definition above and only use the other model alias crossdata which you've already defined in config.xml:

<crossdata>
    <class>MyPackage_Crossdata_Model</class>
    <resourceModel>crossdata_mysql4</resourceModel>
</crossdata>   

Then change the event definition to:

<add_to_cart_after>
    <class>crossdata/observer</class>
    <method>hookToAddToCartAfter</method>
</add_to_cart_after>

Create a file /app/code/local/MyPackage/Crossdata/Model/Observer.php and define your observer class in it:

class MyPackage_Crossdata_Model_Observer
{
    public function hookToAddToCartAfter($observer)
    {
        // your observer code
    }
}
0
votes

You added a folder for Observer.php, or a file? Can you post the code you wrote for this so we can better help debug with you? That tutorial is about as simple and straightforward as they get.

0
votes
  1. In your config.xml the short name given to your blocks and models is crossdata. Yet the event uses the alias dispatcher/observer, you have not defined a name dispatcher. This probably needs to be crossdata/observer.

  2. The Inchoo example calls it's module Inchoo_Dispatcher and the model is Inchoo_Dispatcher_Model_Observer. Your module is MyPackage_Crossdata which means the alias crossdata/observer would try to load MyPackage_Crossdata_Model_Observer. But your observer is erroneously called MyPackage_Crossdata_Dispatcher_Model_Observer.

  3. If your pasted code is not showing here in Stack Overflow either indent it by four spaces or use the {} button to format it. That is the preferable way to show code.