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.


Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));

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.


<adminhtml>
	<controller_action_postdispatch>
		<observers>
			<add_event_one>
				<type>singleton</type>
				<class>yourmodule/observer</class>
				<method>addEventOne</method>
			</add_event_one>
		</observers>
	</controller_action_postdispatch>

	<checkout_submit_all_after>
		<observers>
			<yourmodule_place_order_observer>
				<type>singleton</type>
				<class>yourmodule/observer</class>
				<method>yourCustomFunction</method>
			</yourmodule_place_order_observer>
		</observers>
	</checkout_submit_all_after>
</adminhtml>

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.


public function addEventOne($observer)
{ 
	// Checking full controller and function name
	if($observer->getEvent()->getControllerAction()->getFullActionName() == 'adminhtml_sales_order_create_save') { 
		// Get the single latest order
		$order = Mage::getModel('sales/order')->getCollection()
				 ->setOrder('created_at','DESC')
				 ->setPageSize(1)
				 ->setCurPage(1)
				 ->getFirstItem();				
				 
		$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();		
		
		Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $quote));
	}
	return $this;
}

public function yourCustomFunction($observer)
{ 
	$order = $observer->getEvent()->getOrder();
	$quote = $observer->getEvent()->getQuote();

	// YOUR CODE HERE
	// This is the function where you write code 
	// for the newly created event 'checkout_submit_all_after'
}

Hope it helps. Thanks.