Search code examples
shopware6

Is it okay to fire the CheckoutOrderPlacedEvent a second time?


I am debugging an incompatibility problem of two Shopware 6 modules.

  • One is generating coupon codes in the CheckoutOrderPlacedEvent
  • The other one if firing this event itself

This causes the codes to be generated twice, for the profit of the customer :-)

In which module is this best fixed?

I tend to say, that we should not fire the CheckoutOrderPlaceEvent in modules, because this is likely to cause side effects.

On the other hand it would be possible to fix it in the coupon module, to detect if the coupons were already generated.

I tend to say, that this event shall only be fired once, as also OrderPlaced flows would be executed twice.


Solution

  • I agree that CheckoutOrderPlaceEvent should only be dispatched once, as the name suggests, when the order is placed. Dispatching the event will also trigger flows from the Flow Builder, so by default another order confirmation mail would be sent, which probably isn't in the interest of most merchants. In addition there might be numerous other flows triggered by the event which might do all kind of things. This might also lead to unexpected behavior. Really the keyword here is expectation and for unsuspecting users dispatching the event multiple times would not meet their expectations. While there is a context state to skip the execution of flows, other plugins might listen to the event as well with the expectation, that it is only fired once in the lifecycle of an order.