Magento Events and Observers

 To separate your custom code from the core code we use event driven programming where an event is fired and an observer is listening for specific events and executing certain logics on specific event. These events have certain class objects attached as function parameter .Observers (Class functions) runs when the event is fired and receive the class objects. The observer manipulates and receives data from class objects (event) and performs the operation on data according to requirement.

To create some observer on a specific event we need to create our own module and

event in config.xml of the module as below.

We will define the event under events tags in config.xml and under observer tag we need to define the name of the method call after event fire

Suppose we need send an custom email after every successful order, then we

 

Need to call the observer on checkout_onepage_controller_sucess_action and we have to define it in config.xml file of our module.

File: app/code/local/Letsknowit/Checkout/etc/config.xml <global>
    <events>
      <checkout_onepage_controller_success_action>   //Event Name
       <observers>
          <sendcustomemail>   //Name of observer
<type>singleton</type>
              <class>Letsknowit_Checkout_Model_Observer</class>
              <method>sendcustomemail</method>
        </sendcustomemail>
      </observers>
     </checkout_onepage_controller_success_action>
  </events>

 

</global>

Now in our custom module we need to define our observer function in Observer.php

 

File: app/code/local/Letsknowit/Checkout/Model/Observer.php class Letsknowit_Postdata_Model_Checkout_Observer

{

public function sendcustomemail(Varien_Event_Observer $observer){

 

     $event->getEvent()->getObject1();

}

After every successful order the observer function call and will do the required functionality

 

0   0
Aditya Singh

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site