0

Is it possible for one custom observer to listen to more than one event?

I have a custom observer which right now is listening to only one event, I would like to add 1 or more events for that observer to listen to.

Is it possible or should I create a new observer for each event I want to listen?

  • I think you can filter with event name like $observer->getEvent()->getName(), – Keyur Shah May 23 '17 at 8:19
  • magento 1 or 2? – Marius May 23 '17 at 8:52
1

Yes it is possible, but i'd prefer serveral (observer) methods as long it should not do exactly the same.

<events>
    <event_one>
        <observers>
            <your_module_event_one>
                <class>your_module/observer</class>
                <method>yourMethod</method>
            </your_module_event_one>
        </observers>
    </event_one>
    <event_two>
        <observers>
            <your_module_event_two>
                <class>your_module/observer</class>
                <method>yourMethod</method>
            </your_module_event_two>
        </observers>
    </event_two>
</events>
  • Yes, the above works. I found that out just couple of minutes ago. But right now I actually need the methods to be the exact same/do the same thing. Basically at the moment I'm sending the selected/clicked product's data to somewhere, but I also need to do that when this is item is added to the cart or bought. – anthony May 23 '17 at 8:29
  • Well I found the solution for my problem. Just had to declare different events I want to listen in the config.xml file and keep everything else in the <observers> tag the same. – anthony May 23 '17 at 8:40
  • That is what i've posted above? ;) – sv3n May 23 '17 at 8:45
  • Yes :) I have no idea how I couldn't notice that Your solution is exactly the same. – anthony May 23 '17 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.