Events are very powerful tool in magento for customization.
I my experience, events should be used more often that overriding of classes. The reason being, if there are two or more module, overriding same class then there will be conflict, i.e only 1 module will work. But, if you’re using events, multiple modules can subscribe to a single event easily.
Magento Events follow Observer Design Pattern. So here is how it works: In the core code of magento, at many places magento fires events. Each event has a unique name and some additional data parameters associated with it. In our module, we can subscribe to these events, meanings, as soon as magneto fires an event, a function in our custom module class is executed where we can do some data manipulation and other thing.
Here is how we do it, Magneto uses a function called Mage::dispatchEvent to fire an event. You will find this any many places inside magento code based.
For example in class Mage_Checkout_Model_Type_Onepage in saveOrder function.
Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote()));
Each event has a name and data, in the above case its checkout_type_onepage_save_order_after and data i.e array(‘order’=>$order, ‘quote’=>$this->getQuote()) (which is an array passed)
In our module to subscribe/listen to this event we need to add the following in config.xml file.
Inside the
<events> <checkout_type_onepage_save_order_after> <!-- Name of Event --> <observers> <save_after> <!-- Any Unique Identifier --> <type>singleton</type> <class>Excellence_Test_Model_Observer</class> <!-- Over Model Class --> <method>saveOrderAfter</method> <!-- name of function --> </save_after> </observers> </checkout_type_onepage_save_order_after> </events>
Then we will create a class in Model folder of our module called Observer.php and declare a function saveOrderAfter() there.
<?php class Excellence_Test_Model_Observer { public function saveOrderAfter($evt){ $order = $evt->getOrder(); //this is how we access data passed in event $quote = $evt->getQuote(); //this is how we access data passed in event .... do something here send email etc etc .... } }
Few important events that are fired in magento.
When every a Model is saved an event is fired called
Mage::dispatchEvent(‘model_save_before’, array(‘object’=>$this));
Mage::dispatchEvent(‘model_save_after’, array(‘object’=>$this));
Create a small user tracking system. As soon as a user login in magneto, make a database entry of date time and username of the user. And as soon, as user logs out, make another database entry to record when user logged out.