Understanding How Magento Plugin Execution Order Really Works

Understanding How Magento Plugin Execution Order Really Works

Magento plugin execution can be confusing, especially when multiple plugins target the same method. Understanding how Magento handles before, after, and around plugins — and how it decides the execution order — is critical for building reliable, predictable extensions. This guide breaks down the real behavior of plugin execution order, highlights common mistakes, and explains the role of sortOrder values and nested plugin calls. Whether you’re troubleshooting unexpected results or designing a clean plugin architecture, this detailed breakdown will help you navigate Magento's plugin system with clarity and confidence.

Understanding How Magento Plugin Execution Order Really Works

Magento plugin execution order can be confusing, especially when around plugins enter the picture. If you've worked with Magento plugins before, you know they're vital for customizing behavior without changing core files. But understanding exactly how Magento decides which plugin runs first is less obvious — and has caused mistakes in many Magento projects before. This guide clears it up.

Quick Refresher: What Magento Plugins Are

Magento plugins modify class behavior without overriding the whole class. There are three types:

Plugin Type When It Runs Purpose
Before Plugin Before the original method Can change input arguments
After Plugin After the original method Can change the return value
Around Plugin Wraps the method Full control: can run before, after, or skip the original method

Plugins use the sortOrder attribute in di.xml to control execution sequence. Lower sortOrder means higher priority.

Magento plugin execution order matters a lot when multiple plugins target the same method.

Standard Execution Without Around Plugins

When you have multiple before and after plugins, Magento executes them in order of their sortOrder:

  • Lower sortOrder runs first in "before" methods.
  • Higher sortOrder runs first in "after" methods (in reverse).
Plugin sortOrder Execution
Plugin A 10 beforeExecute() first, afterExecute() last
Plugin B 20 beforeExecute() second, afterExecute() second
Plugin C 30 beforeExecute() third, afterExecute() first

This is logical and expected.

Around Plugins Break the Flow

In Magento, around plugins don't just surround the original method — they restart the plugin execution chain. When an around plugin calls $proceed(), it triggers all remaining plugins, not just the original method.

Example output from a real module:

A::beforeExecute

B::beforeExecute

B::aroundExecute before

C::beforeExecute

C::afterExecute

B::aroundExecute proceed

B::aroundExecute after

A::afterExecute

B::afterExecute

Notice:

  • Plugin B’s around method starts.
  • Inside it, Plugin C’s before and after methods run.
  • Only then does Plugin B’s around method finish.

This nesting behavior surprises many developers (including myself previously) and leads to execution bugs.

Three Key Rules for Magento Plugin Execution

After testing and working through mistakes, these are the reliable rules for Magento plugin execution order:

  • Lower sortOrder executes first.
  • Negative sortOrders are allowed and run before positive ones.

  • Execution type matters: before → around → after.
  • Magento processes all before plugins first, then around plugins, then after plugins.

  • Around plugins reset the execution flow.
  • When $proceed() is called inside an around plugin, Magento restarts the plugin chain for remaining plugins.

Understanding these rules prevents unpredictable bugs when multiple plugins target the same method.

Example Module Setup

Here’s a simple example with three plugins:

<type name="Magento\Cms\Controller\Index\Index">

<plugin name="vendor_module_a" type="Vendor\Module\Plugin\A" sortOrder="10"/>

<plugin name="vendor_module_b" type="Vendor\Module\Plugin\B" sortOrder="20"/>

<plugin name="vendor_module_c" type="Vendor\Module\Plugin\C" sortOrder="30"/>

</type>

And the PHP plugin files (Plugin/A.php, Plugin/B.php, Plugin/C.php) just dump() their methods at key points.

The $proceed call inside Plugin B’s aroundExecute() triggers Plugin C’s full execution.

Why Around Plugins Can Cause Problems

  • They are not simple wrappers.
  • They introduce nesting, not simple chaining.
  • If $proceed() is skipped or misplaced, it can skip other plugins completely.
  • Debugging becomes harder because the sequence isn’t linear anymore.

Magento's documentation doesn't highlight this well enough, which caused confusion in earlier projects.

Best Practices for Working With Magento Plugin Execution

Here’s the cleanest way to manage Magento plugins:

Best Practice Why It Matters
Avoid around plugins unless absolutely necessary They add complexity and debugging pain
Use specific sortOrder only when needed Easier for others to extend or override plugins
Focus each plugin on one task Simpler, clearer codebase
Always document around plugins clearly Explain why and where you use $proceed
Test with real-world scenarios Execution order can change under different conditions

Understanding Magento plugin execution order deeply saves you hours of debugging later. Around plugins create nested plugin flows, not just simple wraps — this mistake caused many past Magento issues.

Tip

To enhance your eCommerce store’s performance with Magento, focus on optimizing site speed by utilizing Emmo themes and extensions. These tools are designed for efficiency, ensuring your website loads quickly and provides a smooth user experience. Start leveraging Emmo's powerful solutions today to boost customer satisfaction and drive sales!

FAQs

What is plugin execution order in Magento 2?

Plugin execution order determines the sequence in which multiple plugins attached to the same method are triggered. Understanding the order is crucial to avoid conflicts and ensure correct functionality.

How does Magento 2 decide the plugin execution order?

Magento uses the `sortOrder` value defined in the plugin's `di.xml` file to determine the sequence. Lower `sortOrder` values execute before higher ones.

What happens if two plugins have the same sortOrder?

If two plugins have the same `sortOrder`, Magento executes them in the order they are declared in the `di.xml` files. However, it's recommended to assign unique `sortOrder` values to avoid unpredictability.

Can I control plugin order without sortOrder?

No. `sortOrder` is the only way Magento manages the sequence. Without properly setting `sortOrder`, plugin behavior can become unpredictable.

What is the default sortOrder value in Magento 2 plugins?

If `sortOrder` is not specified, Magento internally treats it as 0. Always setting an explicit value is a good practice to avoid unintentional conflicts.

Does plugin type affect execution order?

Yes. Magento processes plugins by type: `before` methods run first, `around` methods wrap the original call, and `after` methods run last. Within each type, `sortOrder` controls the sequence.

How do before, around, and after plugins differ in order?

`Before` plugins run in order of their `sortOrder`, followed by `around` plugins (which can wrap the call multiple times), and finally `after` plugins execute, also in `sortOrder` sequence.

What issues arise from incorrect plugin ordering?

Incorrect ordering can cause unexpected results, overwritten data, duplicated logic execution, and serious functional conflicts, especially when multiple modules customize the same methods.

How do I debug plugin execution order in Magento 2?

You can enable Magento's debug mode, use logging inside plugins, or check generated code in the `var/di` and `generated` directories to understand the actual compiled execution order.

Is there a way to visualize the plugin execution flow?

Yes. Tools like Xdebug allow you to step through plugin execution during runtime, giving you a clear view of how methods and plugins interact inside Magento 2.

Does plugin execution order impact performance?

Yes, indirectly. Poorly ordered or unnecessary plugins can introduce overhead, slow down method execution, and increase server load during request handling.

Can I prioritize a custom plugin over Magento core plugins?

Absolutely. By assigning a lower `sortOrder` than the core plugin, you can ensure your custom plugin executes before the core one for the targeted method.

Is it possible to disable a plugin instead of adjusting order?

Yes. You can disable a plugin by setting the `disabled` flag in your module’s `di.xml` file, which is often better than struggling with complex sortOrder adjustments.