Event and Observer in Magento

There is an Event-Observer methodology used in Magento, Magento has been programmed to raise events in crucial areas of the flow.

We can use these events for our requirement.

I am describing here a way to use it. An example would be the event ‘checkout_onepage_controller_success_action’ (this has been use by me at many instances) which will be raised by Magento immediately after an order is placed succesfully. There are many events used in Magento, you can use them as per your requirement.

Event An Event is something that occurs in a certain place during a particular sequence flow.

Observer An Observer is an event handler. It listens to any event it is attached to and accordingly reacts to the event.

I am assuming you are familiar with devloping an module.

Lets begin.
1)I am using the event found in ‘/app/code/core/Mage/Checkout/controllers/OnepageController.php’ in function ‘successAction()’. This is the syntax

Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));

You can also create a custom event, only thing is you need to maintain the namespace.

2) Register the event with its Observer.
In the config file of your module (mine module path app/code/local/DW/Ordercustomer/etc/config.xml), please copy the following code
Suppose my module is ‘ordercustomer’, and ‘saveCustomerInfo’ this is the method to be called on order success

<?xml version="1.0"?>
<config>
<global>
	<!-- for observer-->
	<!--start-->
	<events>           
        <checkout_onepage_controller_success_action>
            <observers>
                <ordercustomer>
                    <class>dw/observer</class>
                    <method>saveCustomerInfo</method>
                </ordercustomer>
            </observers>
        </checkout_onepage_controller_success_action>                 
    </events>      
    <!--end-->
</global>
</config>

3) Create an observer.
My directory structure – app/code/local/DW/Ordercustomer/Model/. Create a file Observer.php here.

Paste the below mentioned code.

<?php

class DW_Ordercustomer_Model_Observer 
{    
    public function saveCustomerInfo($observer) 
    {    	
    	$order_ids = $observer->getEvent()->getOrderIds(); //$order_ids is the array containg the od of the order placed
    	//your code goes here    	
    	return $this; 
    }
}    	

Hope this is helpful.

5 thoughts on “Event and Observer in Magento

  1. hey, this information is really useful.

    Event and observer in magento has special role to make our e-commerce site more functionable.

  2. Hey, thanks so much for the info, I found it really helpful! However, I do have a few questions, hope you can help me.

    First, there are some tutorials on the web that use for the observer’s the literal class name. That is to say,your using
    dw/observer

    would be written by their info
    DW_Ordercustomer_Model_Observer
    Can you please tell me the difference between them?

    Second, in your case wouldn’t have been correct to use the orderCustomer/observer ? I mean to user the module name instead of the namespace name.

    I am sorry if my observation is not correct, I am just working through this stuff and would really appreciate your answer!
    Thanks!

Leave a Reply to Event and Observer in Magento | Technology tutorials Cancel reply

Your email address will not be published. Required fields are marked *

Scroll to top