Home » Magento30 December 2013

Magento: Create your own custom Event Observer

Event-Observer methodology is a very helpful way for customizing Magento. It’s an alternative to overriding Magento core classes/methods.

Magento has raised event at many useful places. However, there can be a situation where there is no event present in any particular method/function and we want an event to be raised there.

Moreover, some events that are present in recent version of Magento are not available in older versions.

In these situations, Magento has provided us the facility to create and use our own custom event and observer.

The following event is dispatched in Model class app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php.

This event is present in Magento version 1.7.0.2. However, it is not available in Magento version 1.4.0.1. Now, if we want checkout_submit_all_after event in Magento 1.4.0.1 then we have to create it by ourselves through a custom module. The code below does so.

config.xml

Note from the below code that:

– You should put event observer code inside adminhtml node/element because the event is created in admin section. If you are creating event for frontend section, then you can keep the event observer code inside global node/element.

– controller_action_postdispatch and controller_action_predispatch are two elements used to create custom events. If controller_action_predispatch is used then the new event created will be triggered at the beginning of the function. If controller_action_postdispatch is used then the new event created will be triggered after all the code in the function is run.

– add_event_one element can have any name. Similarly, addEventOne method can have any name you wish.

Model/Observer.php

Note from the below code that:

– adminhtml_sales_order_create_save refers to saveAction() function of app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php class

– In saveAction() function of app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php class, there is call for createOrder() function of app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php class.

Hope it helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.