0

I am trying to understand about the events and observers in magento.

I created an observer to listen to an event in the checkout page. I have configured it and it triggers after the place order button is pressed.

My doubt is the observer running in background or in foreground?

Note : I saw that the error in the observer stops the order execution.

One more question I have:

If it runs in foreground, what if I have more than one observer which configured to use the same event. What will be the order of execution?

8
  • It depends on the dispatched event for which you have created an observer. Commented May 16, 2024 at 12:33
  • Foreground is the short answer. Follow where dispatchEvent is used in the codebase Commented May 16, 2024 at 19:06
  • @HerveTribouilloy looks like you are correct. But if i want to run the observer in the background is there any way to do that? Commented May 17, 2024 at 9:13
  • use crons, I suspect I would need to understand the context of your query. You need to add more details Commented May 17, 2024 at 9:31
  • 1
    Message queues is likely what you are after then Commented May 18, 2024 at 6:23

2 Answers 2

0
  1. Observer runs in foreground only. It will stop if any exception occurs.

  2. To control the order use Plugins instead of Observers, where before, after and around orders are there.

  3. If the execution is need to be asynchronised, use Message Queues. refer:use message queues in magento2

  4. Sort order is not possible in events.xml

Note: This is a compiled answer from above comments. Thanks herve and jigar

0

Yes, it's run in the foreground if more than one event defined. It's not fixed which event is called first

But if you specified sort order in events.xml, then it called base on this sort order.

1
  • Sort order is possible in events.xml? Commented Jul 24, 2024 at 7:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.