The plugin sort order

Looking back, when we defined a plugin in the di.xml file, one of the attributes that we set for every plugin definition was sortOrder. It was set to 100, 200 to 300 for foggyPlugin1, foggyPlugin2 and foggyPlugin3 respectively.

The flow of the code execution for the preceding plugins is as follows:

  • Plugin1 - beforeGetAddToCartUrl
  • Plugin1 - aroundGetAddToCartUrl
  • Plugin2 - beforeGetAddToCartUrl
  • Plugin2 - aroundGetAddToCartUrl
  • Plugin3 - beforeGetAddToCartUrl
  • Plugin3 - aroundGetAddToCartUrl
  • Plugin3 - afterGetAddToCartUrl
  • Plugin2 - afterGetAddToCartUrl
  • Plugin1 - afterGetAddToCartUrl

In other words, if multiple plugins are listening to the same method, the following execution order is used:

  • The before plugin functions with the lowest sortOrder value
  • The around plugin functions with the lowest sortOrder value
  • The before plugin functions following the sortOrder value from the lowest to the highest
  • The around plugin functions following the sortOrder value from the lowest to the highest
  • The after plugin functions with the highest sortOrder value
  • The after plugin functions following the sortOrder value from the highest to the lowest

Note

Special care needs to be taken when it comes to the around listener, as it is the only listener that needs to return a value. If we omit the return value, we risk breaking the execution flow in such a way that the other around plugins for the same method won't be executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114