How to disable Magento event

There are some cases where you will wish to disable Magento event. For example, in one our projects we decided to disable all “Mage_Log” events in order to decrease number of sql queries to the database. We will describe the process of doing it in this post, but I’m sure similar approach can be used in many other scenarios.

How are Magento events defined? For example, go to the config.xml file of magento module “Mage_Log” and you will see next content in tag “events”.
“Events” tag contains children events, you can see chunk of config.xml file.

<frontend>
	<events>
            <controller_action_predispatch>
                <observers>
                    <log>
                        <class>log/visitor</class>
                        <method>initByRequest</method>
                    </log>
                </observers>
            </controller_action_predispatch>
            .... other events
        </events>
</frontend>

For example, if you want to disable event “controller_action_predispatch” you need to create your own module and put it in your config.xml file next xml content:

	<frontend>
		<events>
			<controller_action_predispatch>
				<observers><log><type>disabled</type></log></observers>
			</controller_action_predispatch>
		</events>
	</frontend>

You will notice that we added tag “type” with value “disable“, explanation is below.
Go to Magento class “Mage_Core_Model_App” and in method “dispatchEvent” you will see next code:

            foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
                $observer->setData(array('event'=>$event));
                Varien_Profiler::start('OBSERVER: '.$obsName);
                switch ($obs['type']) {
                    case 'disabled': // if we set disabled type, event will not be executed.
                        break;
                    case 'object': case 'model':
                        $method = $obs['method'];
                        $observer->addData($args);
                        $object = Mage::getModel($obs['model']);
                        $this->_callObserverMethod($object, $method, $observer);
                        break;
                    default:
                        $method = $obs['method'];
                        $observer->addData($args);
                        $object = Mage::getSingleton($obs['model']);
                        $this->_callObserverMethod($object, $method, $observer);
                        break;
                }
                Varien_Profiler::stop('OBSERVER: '.$obsName);
            }

If you set event type to “disabled“, event will not be executed.

that’s it 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Victoria Yashchuk from Atwix is coming to #MM18HR stage and is ready to discuss “Latest eCommerce trends and popular features for your Magento store” Borna Kraljevic
, | 0

Victoria Yashchuk from Atwix is coming to #MM18HR stage and is ready to discuss “Latest eCommerce trends and popular features for your Magento store”

Magento Master, Óscar Recio from Interactiv4 is coming to #MM18HR and will be talking about “Zero Downtime in Magento 2, It’s Possible?” Borna Kraljevic
, | 0

Magento Master, Óscar Recio from Interactiv4 is coming to #MM18HR and will be talking about “Zero Downtime in Magento 2, It’s Possible?”

Mladen Lotar will be joining us on #MM18HR stage with “Production grade PWA” Borna Kraljevic
, | 0

Mladen Lotar will be joining us on #MM18HR stage with “Production grade PWA”

4 comments

  1. 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.

  2. I’ve used this method with 1.3.2.4 and this actually breaks the compare products functionality.. I also had controller_action_postdispatch set to disabled..

    could have been the one to break it but I’m not sure..

    any thoughts?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.