Disable an event observer defined by default in Magento

Another simple trick. This time we will see how to disable a default event observer. This can be useful if you want to prevent a method to be executed when Mage::dispatchEvent() is called, or simply if you want to define the behavior you want by writing your own observer.

We will see for example how to disable the report creation when a product is added to the wishlist.
Here’s how the observer is defined in app/code/core/Mage/Reports/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Mage_Reports>
            <version>0.7.10</version>
        </Mage_Reports>
    </modules>
    <frontend>
        <events>
            <wishlist_add_product>
                <observers>
                    <reports>
                        <class>reports/event_observer</class>
                        <method>wishlistAddProduct</method>
                    </reports>
                </observers>
            </wishlist_add_product>
        </events>
    </frontend>
</config>

To disable it, you just have to modify the config.xml file of your extension like below:

<?xml version="1.0"?>
<config>
    <modules>
        <MyNamespace_MyModule>
            <version>0.1.0</version>
        </MyNamespace_MyModule>
    </modules>
    <frontend> <!-- Be careful to use <frontend> like above, <global> would not work -->
        <events>
            <wishlist_add_product>
                <observers>
                    <reports>
                        <type>disabled</type>
                    </reports>
                </observers>
            </wishlist_add_product>
        </events>
    </frontend>
</config>

Attention! You have to use the same scope in order to disable or override a default event observer properly. If you use <global> in your extension and Magento defines the event observer in <frontend>, it won’t work.

5 Responses to “Disable an event observer defined by default in Magento”

  1. atwix says:

    Hi! Thanks for sharing your solution. This is a great post. We have an article on similar topic posted here http://www.atwix.com/magento/observers-reality-or-fiction/
    you’re welcome to check it out and leave your feedback.

  2. CG says:

    Hi,
    thanks helped a lot. We had to disable a observer function from a extension, which was causing heavy load on the server.

    So far the extension still works as intendet.

  3. sreenu says:

    i created a custom menu and submenus
    in that i want groups and fields am not getting not in the
    please help me
    thank you

  4. Mark Won says:

    This was very helpful. I was wondering why a normal rewrite to a 3rd party observer wasn’t working. The information here allowed me to figure it out.

    Thanks for posting it!

  5. James Simpson says:

    Can we disable an observer based on page – I.E. I am doing some profilling at the moment, and on the cart page, configurable swatches are letting the load speed down, but they are not used on the cart page at all, so in theory, if i disable the observer on the cart page, it will save 3 seconds on the page load time.

Leave a Reply

* Required fields

Categories