Search code examples
phpmagentoobservers

Prevent the trigger of observer in Magento


I think this is a really easy question but given my level in programming it's not that easy for me.

I wonder if there is a way of stopping or don't trigger an observer after some event ocurrs, this is because the observer that I am doing is in an infinite loop and I want to stop it from doing that loop.

I hope it's my question is clearly enough.

Thanks

UPDATE: An example could be: The observer is trigger in the event: core_config_data_save_after and when this observer is started you want to update a value in the core_config_data table, but when you save it the observer is started again and there is the loop


Solution

  • One possible solution would be to use the registry and set a flag - you simply return from your observer should the registry flag be set on any future hits.

    i.e.

    public function myObserver(Varien_Event_Observer $observer)
    {
        if (Mage::registry('my_observer_has_run')) {
            return $this;
        }
    
        .... Your Code Here ....
    
        Mage::register('my_observer_has_run', true);
    }