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"
Step 2. Now Create Module Configuration File inside the directory /app/code/local/ExtendTree/SimpleObservers/etc as name "config.xml"
0.0.1
singleton
ExtendTree_SimpleObservers_Model_Observer
controller_front_init_before
singleton
ExtendTree_SimpleObservers_Model_Observer
customer_register_success
singleton
ExtendTree_SimpleObservers_Model_Observer
sales_quote_add_item
singleton
ExtendTree_SimpleObservers_Model_Observer
sales_order_place_after
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
Thank you..!!
"The easy way for everything."
Thank you for sharing excellent information. Your website is very cool. Fully useful your blog post... Online Shopping Website In Gujarat
ReplyDeleteExcellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Magento 2 Extension List
ReplyDelete