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.
PHP Code:
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 tag, will put this
HTML Code:
<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 Code:
<?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
PHP Code:
Mage::dispatchEvent(‘model_save_before’, array(‘object’=>$this));
Mage::dispatchEvent(‘model_save_after’, array(‘object’=>$this));
View more threads in the same category:
Bookmarks