Sunday, July 15, 2012

Magento Observer and Dispacting Events

What is Event?
An Event is something that occurs in a certain place during a particular sequence flow. Say once a customer completes an order, the flow sequence would be to

1. Save the order details 2. Send e-mail confirmation to customer

may be more like before loading a database model, after loading a database model, before saving the cart object and many more

Events may be emitted before or after each of these flow points to introduce custom logic.



What is Observer?
Observer An Observer is an event handler. It listens to any event it is attached to and accordingly reacts to the event.

So how do you hook into this event that is dispatched?
When you want to use Magento event-driven architecture you must know basically two things - how to dispatch an event and how to catch it.

Within Magento you can dispatch an event as simple as by calling Mage::dispatchEvent(...) method, for example:
Mage::dispatchEvent('custom_event', array('object'=>$this));
For example:

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

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

Mage::dispatchEvent('adminhtml_catalog_category_tabs', array('tabs'  => $this)); 

This methods accepts two parameters - event unique identifier and associative array of data that is set as Varien_Event_Observer object data, so in fact passed to event observers.

Register a new custom local module under name ‘tech’. For this create file ‘Xyz.xml’ under directory ‘app/etc/modules/’. File contents are -

Create a New custom Module name 'Xyz' .For this create file ‘Xyz.xml’ under directory "app/etc/modules/".
<?xml version="1.0"?>
<config>
  <modules>
    <Xyz_ACustomModul>
      <codePool>local</codePool>
      <active>true</active>
    </Xyz_ACustomModul>
  </modules>
</config>

Register the event with its Observer. Create file ‘config.xml’ under directory ‘app/code/local/Xyz/
custommodule/etc/’ with contents as
<config>
  <global>
    <models>
        <xyzacustommodule>
             <class>Xyz_ACustomModule_Model</class>
        </xyzacustommodule>
    </models>
    <events>
      <custom_event>
        <observers>
          <xyz_acustommodule_model_observer>
            <type>singleton</type>
            <class>Xyzacustommodule/Observer</class>
            <method>customObserverAction</method>
          </xyz_acustommodule_model_observer>
        </observers>
      </custom_event>   
    </events>
  </global>
</config>

Parameter Explanation
<global> - If you want your observer to listen no matter where the event is dispatched from, put it here. You can also put it in "frontend" or "adminhtml".
<events> - This is the element that stores all of the events that are registered.
<custom_event> - This is the "event" that you are listening to.
<observers> - This is the type of event. I don't think there are others.
<xyz_acustommodule_model_observer> - This is a unique string that defines this configuration. It can be anything, and just needs to be unique.
<type> - I have always used singleton, but other options can be "model" or "object". The "singleton" will create the object as Mage::getSingleton() while both "object" and "model" will use Mage::getModel() when creating the observer object.
<class> - This is the observer class.
<method> - This is the function to be called in the observer class.

Creating the Observer. Create the directory structure - app/code/local/Xyz/ACustomModule/Model. Place the php code below in a file by name ‘Observer.php’ in the directory just created.
class Xyz_ACustomModule_Model_Observer
{
  public function customObserverAction(Varien_Event_Observer $observer)
  {
    $object = $observer->getEvent()->getObject(); // we are taking the item with 'object' key from array passed to dispatcher
    $object->doSomething();
    return $this;
}

List of Dispacth Events can be found here

No comments:

Post a Comment

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...