Welcome

diaper cake

Magento® Observers

How to use Magneto events observers??

  • Step 1. Create global configuration File
  • Step 2. Create module configuration file
  • Step 3. Create module model observer

 

Step 1. Each Module should have one Namespace & Module name. Name space could be some company name etc. & module name should be something that represents module functionality. So let suppose Namespace is ExtendTree and Module name is SimpleObservers. Now Create Global Configuration File inside root directory /app/etc/modules as name "ExtendTree_SimpleObservers.xml"

 

<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <ExtendTree_SimpleObservers> <active>true</active> <codePool>local</codePool> </ExtendTree_SimpleObservers> </modules> </config>

 

Step 2. Now Create Module Configuration File inside the directory /app/code/local/ExtendTree/SimpleObservers/etc as name "config.xml"

 

<?xml version="1.0"?> <config> <modules> <ExtendTree_SimpleObservers> <version>0.0.1</version> </ExtendTree_SimpleObservers> </modules> <global> <events> <!--Event on each page load in frontend Start--> <controller_front_init_before> <observers> <extendtree_first_observer> <type>singleton</type> <class>ExtendTree_SimpleObservers_Model_Observer</class> <method>controller_front_init_before</method> </extendtree_first_observer> </observers> </controller_front_init_before> <!--Event on each page load in frontend Start--> <!--Event on customer registeration Start--> <customer_register_success> <observers> <extendtree_second_observer> <type>singleton</type> <class>ExtendTree_SimpleObservers_Model_Observer</class> <method>customer_register_success</method> </extendtree_second_observer> </observers> </customer_register_success> <!--Event on customer registeration End--> <!--Event on order place Start--> <sales_quote_add_item> <observers> <extendtree_third_observer> <type>singleton</type> <class>ExtendTree_SimpleObservers_Model_Observer</class> <method>sales_quote_add_item</method> </extendtree_third_observer> </observers> </sales_quote_add_item> <!--Event on order place End--> <!--Event on order place Start--> <sales_order_place_after> <observers> <extendtree_fourth_observer> <type>singleton</type> <class>ExtendTree_SimpleObservers_Model_Observer</class> <method>sales_order_place_after</method> </extendtree_fourth_observer> </observers> </sales_order_place_after> <!--Event on order place End--> </events> </global> </config>

 

Step 3. Now next thing is to create model observer inside the directory /app/code/local/ExtendTree/SimpleObservers/Model/ as name "Observer.php"

 

class ExtendTree_SimpleObservers_Model_Observer { /* * This observer will be called on each page load in frontend. */ public function controller_front_init_before($observer) { Mage::log('Frontend Page loaded', null, 'FirstObserver.log'); } /* * This observer will be called on each Customer registeration. */ public function customer_register_success($observer) { Mage::log('Customer registered', null, 'SecondObserver.log'); } /* * This observer will be called when visitor add product into cart. */ public function sales_quote_add_item($observer) { Mage::log('One Product Added into Cart', null, 'ThirdObserver.log'); } /* * This observer will be called when an order will be placed. */ public function sales_order_place_after($observer) { Mage::log('One order has been placed', null, 'FourthObserver.log'); } }

 

That's it..!! Now try to place an order, customer registeration, add product to cart, load frontend. It should create a log file inside the directory /var/log

 

Click here to get this module for free.

 

 

Thank you..!!

"The easy way for everything."