Shopware 6 - Tips, tricks and how to's

Preventing events from triggering again – alternative solution

This article is a continuation of my previous article Preventing events from triggering again (and again, and again..). Recently, I have found out, that the solution described there does not work in some cases. So I have had to come with an alternative solution and it is actually more simple and ‘pure PHP’. So let’s take a look at it..

In my case, this solution did not work, when Shopware 6 event CustomerBeforeLoginEvent was triggered multiple times, because of its logic, that caused a recursion:

I am not sure, why this works in other cases and not in this one, but a solution to this is basically just saving the status, e.g. “this was already triggered” to the class property (variable, that is available within the whole class). Here is how it looks:

I actually like this solution more, that the other one. The advantages ae quite clear – no dependency injection is needed and we get the ability to define, not just if, but how many times we allow the subscriber to trigger the recursion. Let’s allow it just two times:

I hope that this will help someone. For me, it is a reminder, that sometimes the simplest solution is the best. Well, most times actually. 🙂