Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Magento Event & Observer with Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 71
    Comment on it

    Event & observer is one of the most powerful features which we can use in any custom module. Generally, Magento uses a different kind of events which are fired/dispatched by Magento throughout its code base. Each event has class objects (data array) attached as function parameters & the observer class receive this class object when the certain event is fired. In a simple way, Observer is act like a listener which is used to detect the event. Observer always waits for an event so that it can execute the event whenever the event is fired.

    Magneto uses Mage::dispatchEvent function to fire an event, you can find this inside the core files ofMagentoo.

     

    For Example

    Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote()));


    Each event has a name and data, First Parameter refer to Event Name "checkout_type_onepage_save_order_after" and 2nd parameter refer to data i.e array(‘order’=>$order, ‘quote’=>$this->getQuote())

     

    1. We need to Define observer for 'checkout_type_onepage_save_order_after' event in config.xml file

    <events>
         <checkout_type_onepage_save_order_after>
             <observers>
                       <save_after>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>saveOrderAfter</method>
                      </save_after>
            </observers>
          </checkout_type_onepage_save_order_after>    
    </events>


    2. Now, Create Observer.php file to execute the event observer is located in the Model directory of your module and extends the Varien_Event_Observer class
    Path: /app/code/local/Namespace/Modulename/Model/ folder and create a file Observer.php:

    class Namespace_Modulename_Model_Observer extends Varien_Event_Observer
    {
    
       public function saveOrderAfter($observer)
       {
             $order = $observer->getOrder();
                $quote = $observer->getQuote();
       }
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: