Magento EE 1.10 – Unregister Event Observer Programmatically

ee-1.10event-observer

Does anyone know if it's possible to unregister an event observer programatically? I have an observer on newsletter_subscribe_save_after that updates a custom attribute in the customer model but when the customer record is saved it triggers off the customer_save_after event defined in Mage_All.xml which then re-saves the newsletter subscription status resulting in an infinite loop that then triggers the PHP recursion error due to nesting 100 times.

Ideally I'd like to unregister the customer_save_after event only when my observer fires.

Best Answer

You can try to mark your event as dispatched and on the second attempt of running it just do nothing if it's already dispatched. For example let's say your method is called updateCustomer().

public function updateCustomer($observer){
    if (Mage::registry('my_event_was_dispatched') == 1){//do nothing if the event was dispatched
        return $this;
    }
    //your code here
    Mage::register('my_event_was_dispatched', 1);//mark the event as dispatched.
}

The registry key name is not important, just make sure you use the same one in both cases.

Related Topic