Magento: Event Observer Scope

I learned a valuable lesson this weekend (the hard way of course) when it comes to event observers in Magento. You’ve got 3 different places to setup your event observers in your config.xml file of your modules. They are as follows:

<config>
    <frontend>
        <events>
            ...
        </events>
    <frontend>
    <adminhtml>
        <events>
            ...
        </events>
    <adminhtml>
    <global>
        <events>
            ...
        </events>
    <global>
</config>

It is pretty self-explanatory, but here’s how it works: Events are firing off all the time. If you are needing to observe an event, but only when it happens on the frontend, then you would put your observer in the ‘frontend’ section. That same event might also be fired off in the admin, but your module won’t observe that event from the admin if you have it in ‘frontend’. If you want the event to be observed no matter where it happens (admin or frontend), you would then put your observer in the ‘global’ section.

I was trying to observe an event that should be fired off when an order is edited in the admin. I wasn’t able to log anything in my observer at all, and I couldn’t figure out why. Turns out it was because I had the event observer in the ‘frontend’ section. I changed it to ‘adminhtml’ and then it worked just fine.

This entry was posted in Magento. Bookmark the permalink.

9 Responses to Magento: Event Observer Scope

  1. ijaz says:

    Hi,

    i think you should explain it with example.

    Regards,

    Ijaz Ali

  2. Jon says:

    Can you think of any way to target an observer to an event, only when those events take place via Magento’s API?

  3. Shaun says:

    Thanks for the post. I just noticed that some events were under the node, so I thought something like this might be the case. A quick trip to google, led me to this post, saving me some exploratory testing. Thanks again!

  4. me says:

    >>Turns out it was because I had the event observer in the ‘‘ section. I changed it to ‘‘ and then it worked just fine.

    Oh please. You changed what to what?

    I’m guessing you changed frontend or adminhtml to global.

    • Josh Pratt says:

      That was one of the more unique “there’s something wrong with your post” comments I’ve ever received… But, thanks for pointing that out. I fixed it. For some reason, my new site design doesn’t properly recognize code tags.

  5. Ela says:

    Hi,

    Can you please explain how does this events will help . I couldn’t understand it clearly