magento events-magento observer

Howdy guys,

Magento Open Course has occurred for a long time and you already learned a lot of lessons on Magento. Continuously, today we will begin with a new topic – Magento Events. This lesson takes about two hours and covers the following issues:

  • Magento events
  • How to declare Magento observer (listener)
  • Common Magento events.

1. Magento Events

We can consider an event as a kind of flag that rises when a specific situation happens. For example, when a user presses the Place Order button on website, it is an event. Or your account has been created, which is also an event.

For example: Event catalog_controller_category_init_before is raised in file controller app/code/core/Mage/Catalog/controllers/CategoryController.php before initializing category object.

class Mage_Catalog_CategoryController extends Mage_Core_Controller_Front_Action</span>

{
/**
* Initialize requested category object
*
* @return Mage_Catalog_Model_Category
*/
protected function _initCategory()
{
Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' =&gt; $this));
...
}
}

In the above example, function Mage::dispatchEvent() raises a new event. It calls all observer callbacks registered for this event and multiple observers matching event name pattern.

Variables in the Mage::dispatchEvent() function include:

  • catalog_controller_category_init_before: Event name pattern
  • array(‘controller_action’ => $this): Data supplied for observers to listen to this event

2.Magento Observer

2.1. Definition

An observer is as a listener. It listens to your program to detect events which match name pattern. When there is an event taking place, all observers of that event will be performed.

2.2. How to create a Magento observer

Declare a Magento observer 

The code declared observer is written in file config.xml of module. In this lesson, we need to create a Magestore_Lesson17 module (you can use Module creator). Next, add the following code block to file app/code/local/Magestore/Lesson17/etc/config.xml.

<config>
<frontend>
...

<events>
<catalog_product_get_final_price>
<observers>
<magestore_lesson17_obsever>
<type>singleton</type>
<class>lesson17/observer</class>
<method>getProductFinalPrice</method>
</magestore_lesson17_observer>
</observers>
</catalog_product_get_final_price>
</events>

...
</frontend>
</config>

Note: The above code block (in tags <events></events>) is added to tags <frontend></frontend>, which means that the observer declared will listen to events trigger in frontend. Moreover, we can write the code block used to declare a Magento observer.

3. Common Magento events

In this part we will introduce you to common Magento events used in extension projects.

3.1. Event catalog_product_get_final_price

File path: app/code/core/Mage/Catalog/Model/Product/Type/Price.php

This event allows observers change final price of product.

Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));

3.2. Event catalog_product_collection_apply_limitations_after

File path: app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

This event allows observers filter product collection before showing on frontend.

Mage::dispatchEvent('catalog_product_collection_apply_limitations_after', array('collection' => $this));

3.3. Event checkout_type_onepage_save_order_after

File path: app/code/core/Mage/Checkout/Model/Type/Onepage.php

This event allows observers to get data from Order, Quote or to change data of Order such as add a discount total.

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

3.4. Event controller_action_predispatch

File path: app/code/core/Mage/Core/Controller/Varien/Action.php

This event always occurs when you access into any frontend page of Magento and is often used to carry out global website checkings.

Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $this));

Here is the end of lesson 17, Magento events. After enjoying this lesson, you should gain knowledge of Magento events including basics of Magento events and Magento observer, and common Magento events.

Hope that it is helpful for you.

See you soon. Follow us in Magento Open Course for Magento Tutorial.

Lesson 17 – Magento events
5 (100%) 1 vote

Leave a Reply

Your email address will not be published. Required fields are marked *