Our Work
200+ Enterprises across globally trust KTree for their Web & Mobile application Development needs.See What We Do
Updated today
Magento is the most popular open source eCommerce product. It has thousands of plugins free and paid. To write plugins/custom code/Extensions effectively one need to understand Magento Event-Observer pattern. We are going to see what it is and how can we use it to write a Magento Plugin.
Observers register themselves to wait for an event so that they can execute code whenever an event is fired. In other words each observer registers with the subject, and when an event occurs, the subject notifies and observer code is executed.
An Event is something that is/can be triggered during a particular sequence flow.Say for example once a customer completes an order, the flow sequence would be to
For using Events Observers first we need to declare the events in config.xml under tag.
In Magento Events are divided into three sections:
global - When we are declaring events in the section these events will be accessible both in frontend and Admin.
adminhtml - These are the events which will triggered only in Admin area.
frontend - These are the events which will triggered only for front end section.
Then we need to register an observer method for particular event.
<event_name>
<observers>
<module_name>
<type>typeOfObserver</type>
<class>Observer Class Name</class>
<method>methodName</method>
</module_name>
</observers>
</event_name>
When particular event is triggered related Observer method will execute the code after the specified action.
We can dispatch the events in the following way.
Mage::dispatchEvent(‘eventname’, ‘params Info’);
Example:
eav_entity_attribute_set_save_commit_after
Define the event in the module config file.
app/code/local/Ktree/Eventobservers/etc/config.xml
This event is related to admin as we are only saving the attributeset from Magento admin with default installation. So we need to create event definition under adminhtml tag in config.xml.
Under config section we need to create a new section with name if it is not exists.
<adminhtml>
<events>
<eav_entity_attribute_set_save_commit_after>
<observers>
<ktree_eventobservers_attribute_save_after>
<type>singleton</type>
<class>Ktree_Eventobservers_Model_Observer</class>
<method>onAttributeSetSave</method>
</ktree_eventobservers_attribute_save_after>
</observers>
</eav_entity_attribute_set_save_commit_after>
</events>
</adminhtml>
After defining the event in config.xml we need to create a Model with the following class under the path app/code/codepool/Namespace/Module/Model/Observer.php
<?php
Class Ktree_Eventobservers_Model_Observer
{
public function onAttributeSetSave($observer) {
//here we need to write our own login based on requirement
// getting the attributeSet Data using $observer Object
$attributeSetData = $observer->getEvent()->getDataObject()->getData();
//retrieving the Attributeset form post Data
$syncStatus = Mage::app()->getRequest()->getParam('sync_attr_set_create');
$attributeSetPostData = Mage::helper('core')->jsonDecode(Mage::app()->getRequest()->getParam('data'));
$syncStatus = ($syncStatus) ? $syncStatus : $attributeSetPostData['sync_attr_set_create'];
$attrSetId = $attributeSetData['attribute_set_id'];
//Saving the Custom data into our model like below
$ktEavAttributeSet = Mage::getModel('kteavattributeset/'kteavattributeset);
$ktEavAttributeSet->setAttributeSetId($attrSetId)->setSyncStatus($syncStatus);
$ktEavAttributeSet>save();
return true;
}
}
Few of Magento Events which KTree used in Magento Development.
admin_system_config_section_save_after
In this event we can find the changes related to admin configuration which is in system-> configuration link. Based on the system configuration value we can execute the custom code like sending the same value to Other systems or Validating the custom plugin license with provided Api Key in config section
admin_session_user_login_success
This is the event which we can validate the custom code after successful login of admin user.
adminhtml_widget_container_html_before
In this event observer we can add some extra html code like buttons to specific widget Area without overriding the core magento admin blocks.
core_block_abstract_to_html_after
controller_action_layout_generate_blocks_after
These are the events where we can inject custom html code to adminhtml forms and also in the frontend section.
catalog_product_save_before
customer_save_before
In this event we can add any other values to object based on other attributes information and also we can add server level Validations also.
catalog_product_save_commit_after (Product After Save event)
eav_entity_attribute_set_save_commit_after (AttributeSet After Save event)
catalog_entity_attribute_save_commit_after (Attribute After Save event)
catalog_category_save_commit_after (Category After Save event)
adminhtml_customer_save_after (Customer Save event)
customer_address_save_after (Customer Address Save event)
Here we can check the related object updated attribute information and we can save the data into other modules/ change history.
catalog_product_delete_after (Product Delete Event)
catalog_controller_product_delete (Product Delete Event)
catalog_controller_category_delete (Category Delete Event)
catalog_entity_attribute_delete_after (Attribute Delete Event)
eav_entity_attribute_set_delete_after (AttributeSet Delete Event)
customer_address_delete_after (Customer Address Delete Event)
customer_delete_after (Customer Delete Event)
Here we do some login like removing existing links with other instances.
adminhtml_catalog_product_edit_prepare_form
In this event we can inject block of code after product from elements for displaying information for users.
eav_entity_attribute_set_delete_before
In this event we can find the products related to attribute set and we can perform the same operations like product delete because of all products will remove from Magento if we are going to delete attributeset.
eav_entity_attribute_set_delete_before
This is the event which we can use for Mass update of products. Here we can find what are the attributes are updated and corresponding product ids.
catalog_category_prepare_save
catalog_category_tree_move_before
Here we can add custom validations for category save. Before saving the category we can check same category already exists or not in the current level as we don’t have category unique in default Magento
catalog_category_tree_move_after
In this event we can find the previous and current parents related to the category.
sales_order_save_after
In this event We can find the change in Order Status and we can execute the custom logic based on order status
sales_order_place_after
sales_model_service_quote_submit_after
This event will trigger from frontend place order. We can check any custom payment status and we can save the data into custom database tables for further use.
order_cancel_after
sales_order_creditmemo_save_before
This event will trigger when we are cancelling the order from Magento admin. In this event we can refund the amounts and adding the ordered product stock back to Product.
sales_order_shipment_save_after
This will trigger after shipment created for particular order. Here we can deduct the related products stock from connected instances like Openerp/MicroBiz on the fly using Apis.
sales_quote_remove_item
This will trigger when we are removing an item from cart, so that we can made necessary changes to cart if there is any dependent discounts
sales_quote_collect_totals_after
If there are any custom discounts after applying the gift cards which is saving in another model, We are loading the model data in this observer and recalculating the totals
sales_order_payment_refund
When we are refunding order we need to check is there any gift card items usied while placing the order, if any we need to revert the amount back to GC.
checkout_controller_onepage_save_shipping_method
checkout_controller_multishipping_shipping_post
This event we are using for custom shipping methods which we can find the method is available on the specified date or not on the fly with third party system.
checkout_multishipping_controller_success_action.
This will trigger all the orders are successfully created in multishipping page.
checkout_type_multishipping_create_orders_single
In multi shipping checkout each address will generate single address so that we can find each and every order save using this event in multishipping checkout.
checkout_cart_product_add_after
This event will trigger when we are adding single product to cart. In this event we can add any custom options to product and also we can make changes to product attributes like price configurable options etc
checkout_cart_save_after
checkout_cart_update_items_after
controller_action_predispatch_checkout_cart_updatePost
This event will trigger when cart is updated. So based on cart totals we can add custom discounts based on products available in the cart
sales_quote_load_after
sales_order_load_after
Please Contact us if you have any Magento Implementation requirements. Hire dedicated Magento developers or Magento Development services from KTree. KTree is Best offshore Magento E-commerce development company with extensive experience in E-commerce development and Magento Plugins.
Request For Quote