Magento Event

Magento provides a very useful technique to customize modules which allows us not to override core files. It's called "event". In this article, I will start by providing some basic knowledge about Magento event. Perhaps, lots of people already knew about this but I believe it will be helpful for Magento beginners.

Why and when we use event

Magento also allows you to override files to execute your functions. You can override methods in models or actions in controllers. But sometimes, there are some different modules which override the same file and it can cause a conflict problem. In this case, using event to make your function work is a better way and it is really more popular for all developers.

Event types

There are 2 types of event: pre-defined and custom event. If you look at the event list, you can see that there are events in form: $eventName, $this->_eventPrefix.‘_load_before’, $this->_eventPrefix.‘_load_after’, ... and other events have pre-defined names. That means at the model you extend from which contains these events, you can redefine the event name by overriding the variable name $_eventPrefix or $eventName,... The Magento event list can be found at address:

How to use custom events

As event is quite popular, you can easily find how to use pre-defined event from the internet. In this article, I will just show you how you use event when it is not defined in Magento.

In your extended model, you should redefine a default event variable as:

class Magebuzz_Affiliate_Model_Campaign extends Mage_Core_Model_Abstract
protected $_eventPrefix = 'campaign';
// your variables and methods here

In Mage/Core/Model/Abstract.php file, we have events like :
• $this->_eventPrefix.'_load_before'
• $this->_eventPrefix.'_load_after'
• $this->_eventPrefix.'_save_commit_after'
• $this->_eventPrefix.'_save_before'
• $this->_eventPrefix.'_save_after'
• $this->_eventPrefix.'_delete_before'
• $this->_eventPrefix.'_delete_after'
• $this->_eventPrefix.'_delete_commit_after'
• $this->_eventPrefix.'_clear'

This means, you have the following events:
• 'campaign _load_before '
• 'campaign _load_after'
• 'campaign _save_commit_after'
• 'campaign _save_before'
• 'campaign _save_after'
• 'campaign _delete_before'
• 'campaign _delete_after'
• 'campaign _delete_commit_after'
• 'campaign _clear'

Because this model extends from Mage_Core_Model_Abstract model, so when you react with your data like save, delete, create, read… Your event will be dispatched. You should redefine unique value for event variable in your model. In Magento, many models have $_eventPrefix variable, some of them are:
• Mage_Core_Model_Resource_Db_Collection_Abstract
• Mage_Sales_Model_Resource_Order_Abstract
• Mage_Sales_Model_Quote, .....
• Mage_Core_Controller_Varien_Action

You’d better care about the order of extended sequences. As you can see, both Mage_Catalog_Model_Resource_Category_Collection and Mage_Core_Model_Abstract have $this->_eventPrefix.'_load_after' event. However, the dispatched times are different. If your model extends from Mage_Catalog_Model_Resource_Category_Collection model, your redefined event is dispatched after category collection is loaded or your category collection is loaded. But if your model is extended from Mage_Core_Model_Abstract model, your event is dispatched after you load a model data. As a result, 2 events that have same name could be conflicted. Be careful!

2 thoughts on “Magento Event”

2 Responses to Magento Event

  • Ambika Royan

    Thank Roy..

    I liked your article. i impressed this magento event.A blog can be a very great Event for advertising if use wisely. A blog must provide valuable information. This will make visitors come back once more.

    Ambika Royan

  • Dushyant Joshi
    Dushyant Joshi on December 12, 2012 at 5:11 pm said:

    Thanks for sharing such a useful information