Overriding Magento observers

As you may know, we need to override the Magento observers when we deal with the extension’s conflicts or want to disable (or modify) a part of some extension’s functionality. Usually, there is no difference between the observer’s overriding and overriding of any other regular model. But sometimes we can face with the issues working with the simple override. How should we act then to keep it conventionally? Let’s check the following step by step instruction for Magento observers overriding.

First of all, we should pay attention to the naming. Let’s assume that we have some 3rd party extension. It is named Some_Extension, and this extension has the following observer:

<?php 
/**
 * file path:
 * magento_root/app/code/local/Some/Extension/Model/Observer.php
 */

class Some_Extension_Model_Observer
{
   public function someMethod( $observer )
   {
      /** 
       *Do some stuff here 
       */
        
      return $observer;
   }
}

This observer is catching some event using someMethod(), and it is described in the extension’s config.xml file (note that we are using only the required parameters for this example):

<?xml version="1.0"?>
<!--
/**
 * file path
 * magento_root/app/code/local/Some/Extension/etc/config.xml
 */-->
<config>
    <modules>
        <Some_Extension>
            <version>1.0.0</version>
        </Some_Extension>
    </modules>
    <global>
        <models>
            <some_extension>
                <class>Some_Extension_Model</class>
            </some_extension>
        </models>
        <events>
            <event_name>
                <observers>
                    <some_extension_observer>
                        <type>singleton</type>
                        <class>some_extension/observer</class>
                        <method>someMethod</method>
                    </some_extension_observer>
                </observers>
            </event_name>
        </events>
    </global>
</config>

When we already have a starting point, we need to override the observer’s method – someMethod(). Let’s prepare our extension for this stage:

<?xml version="1.0"?>
<!--
   file path
   magento_root/app/etc/modules/Our_Extension.xml
-->
<config>
    <modules>
        <Our_Extension>
            <active>true</active>
            <codePool>local</codePool>
        </Our_Extension>
    </modules>
</config>
<?xml version="1.0"?>
<!--
/**
 * file path
 * magento_root/app/code/local/Our/Extension/etc/config.xml
 */-->
<config>
    <modules>
        <Our_Extension>
            <version>1.0.0</version>
        </Our_Extension>
    </modules>
    <global>
        <models>
            <our_extension>
                <class>Our_Extension_Model</class>
            </our_extension>
        </models>
    </global>
</config>

In this case, we can make a conventional model rewrite. We need to add the rewrite to the model’s node in config.xml:

        <models>
            <our_extension>
                <class>Our_Extension_Model</class>
            </our_extension>
            <some_extension>
                <rewrite>
                    <observer>Our_Extension_Model_Rewrite_Observer</observer>
                </rewrite>
            </some_extension>
        </models>

Here is the rewritten observer file itself:

<?php
/**
 * file path:
 * magento_root/app/code/local/Our/Extension/Model/Rewrite/Observer.php
 */

class Our_Extension_Model_Rewrite_Observer extends Some_Extension_Model_Observer
{
    public function someMethod( $observer )
    {
        /**
         * Our stuff here
         */

        return $observer;
    }
}

By the way, recently we have faced with the observer declaration that can not be overridden that way:

        <events>
            <event_name>
                <observers>
                    <some_extension_observer>
                        <type>singleton</type>
                        <class>Some_Extension_Model_Observer</class>
                        <method>someMethod</method>
                    </some_extension_observer>
                </observers>
            </event_name>
        </events>

Actually, there are two ways to reach our goal here. And in both ways we need to make our extension dependent on the other extension (which is being overridden) in the Our_Extension.xml:

<?xml version="1.0"?>
<!--
file path
magento_root/app/etc/modules/Our_Extension.xml
-->
<config>
    <modules>
        <Our_Extension>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Some_Extension />
            </depends>
        </Our_Extension>
    </modules>
</config>

The first way is to disable the second extension’s observer and bind our observer to the same event instead. Add the events node to our extension’s config.xml with this content (the events node is included):

        <events>
            <event_name>
                <observers>
                    <some_extension_observer>
                        <type>disabled</type>
                    </some_extension_observer>
                    <our_extension_observer>
                        <type>singleton</type>
                        <class>Our_Extension_Model_Rewrite_Observer</class>
                        <method>someMethod</method>
                    </our_extension_observer>
                </observers>
            </event_name>
        </events>

Furthermore, the second way is to adjust the model call to use pseudonyms in the original observer’s identifier by rewriting it. The whole config.xml of our extension will look like this:

<?xml version="1.0"?>
<!--
/**
 * file path
 * magento_root/app/code/local/Our/Extension/etc/config.xml
 */-->
<config>
    <modules>
        <Our_Extension>
            <version>0.1.0</version>
        </Our_Extension>
    </modules>
    <global>
        <models>
            <our_extension>
                <class>Our_Extension_Model</class>
            </our_extension>
            <some_extension>
                <rewrite>
                    <observer>Our_Extension_Model_Rewrite_Observer</observer>
                </rewrite>
            </some_extension>
        </models>
        <events>
            <event_name>
                <observers>
                    <some_extension_observer>
                        <type>singleton</type>
                        <class>some_extension/observer</class>
                        <method>someMethod</method>
                    </some_extension_observer>
                </observers>
            </event_name>
        </events>
    </global>
</config>

This approach allows us to make the conventional rewrite using a safe way. Moreover, the last thing that we would like to mention is that there was no interference into the Some_Extension code.

We hope that this article will help improve your skills in the programming work. Thank you for reading our blog!