In a code review of a merchant’s site you have discovered that there are multiple observers for the checkout_submit_all_after event. After closer inspection, you notice one of these observers is relying on the others being executed before it is called.
What risk does this pose, and how can it be mitigated?
A. There is no risk posed as long as each event observer specifies a correct sortOrder. Nothing needs to be changed.
B. Event observers are fired in alphabetical order of the observer name. There is no risk here.
C. Magento only supports one observer per event. These observers will need to be combined into a single point of customization.
D. Order of listener execution cannot be guaranteed, resulting in a fragile implementation. This code should be re-worked using plugins
Answer: should be D
Regarding sort order – Magento not allowed sort orders in observer
Regarding the alphabetical order of the observer name – If there are mutiple observers for the event then the Magento 2 behavior becomes unpredictable because we can not set the observer’s ordering.
Regarding the answer C – We have used multiple modules in the project and every module define its own feature so this is wrong
Tested we have arrived the correct answer is C
not true, bobo
I agree with D.
D is correct.
https://devdocs.magento.com/guides/v2.4/ext-best-practices/extension-coding/observers-bp.html
It says:
Do not rely on invocation order
Your observer should not make assumptions about the order in which it will be invoked nor should it rely on the execution of another observer. Observers listening to the same event may be invoked in any order when that event is dispatched.
After a few reviews of this question i now agree that D should be the right answer, as this specifies that. code review is taking place and if you read into the question although the Event observers execute in Alpha order. Theres no guarantee that all observers are named in the correct way to order them correctly. So D would be best answer
I think B is indeed correct because you can guarantee the order by depending obervers on others. Everything else is fired alphabetically, I do agree you should rework using plugins but the statement that the order cannot be guaranteed is not true. I think D is correct, but B is “more” correct
Correct answer should be D