Header Ad

Tuesday, April 3, 2012

Events & Observers in Magento


The following is the command used to get the list of events in magento installed version.

grep -rin -B2 -A2 "Mage::dispatchEvent" app/* > events.txt

In Magentov1.4, nearly 300 events compared to 223 in v1.3 and 140 in v1.2.

The following is the sample example for writing "events".

<events>
  <EVENT_TO_HOOK>
    <observers>
      <module>
        <type>singleton</type>
        <class>company_module_model_observer</class>
        <method>methodToCall</method>
      </module>
    </observers>
  </EVENT_TO_HOOK>  
</events>

Writing multiple observers for same event

We can write multiple observers for same event, like as follows.

    <observers>
      <shipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </shipmentsave>
    </observers>

   <observers>
      <bshipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </bshipmentsave>
    </observers>

If we observe in the above XML tags, we have "<shipmentsave>" and "<bshipmentsave>". Where we can write these tags in different modules respectively.

1 comment:

maria_atwix said...

Hi! Thank you for the post! Discussion of the issue is also described here http://www.atwix.com/magento/observers-reality-or-fiction/
welcome to look through this article and hope that this information will be useful