Magento 2 Plugins (Interceptors)
Before writing code
Fetch live docs: Fetch https://developer.adobe.com/commerce/php/development/components/plugins/ for the official plugins guide with exact method signatures and limitations.
Conceptual Architecture
What Plugins Do
Plugins intercept public method calls on any non-final class, allowing you to modify arguments, return values, or wrap entire method execution — without modifying the original class or using inheritance.
Three Plugin Types
| Type | Method Prefix | Purpose | Receives |
|---|---|---|---|
| Before | before<MethodName> | Modify input arguments | Subject + original args |
| After | after<MethodName> | Modify return value | Subject + result (+ original args) |
| Around | around<MethodName> | Wrap entire execution | Subject + $proceed callable + original args |
Before Plugin
- Method:
beforeOriginalMethod($subject, $arg1, $arg2, ...) - Return: array of modified arguments, or
nullto keep originals - Executes before the original method
After Plugin
- Method:
afterOriginalMethod($subject, $result, ...$args) - Return: modified result
- Executes after the original method
- Since 2.2: receives original arguments after
$result
Around Plugin
- Method:
aroundOriginalMethod($subject, callable $proceed, $arg1, $arg2, ...) - Return: whatever the method should return
- Must call
$proceed($arg1, $arg2)to invoke the original (or skip it) - Use sparingly — most cases are better served by before + after
Plugin Declaration (di.xml)
Plugins are declared as children of a <type> element:
name— unique plugin identifiertype— fully qualified plugin class namesortOrder— execution priority (lower runs first)disabled—trueto disable
Execution Order
- Before plugins execute in sortOrder (ascending)
- Around plugin's pre-
$proceedcode - Original method (via
$proceed) - Around plugin's post-
$proceedcode - After plugins execute in sortOrder (ascending)
Limitations — Cannot Intercept
finalclasses orfinalmethods__construct()(constructor)staticmethods- Non-public methods (
protected,private) - Virtual types (directly)
- Classes instantiated before the interception framework bootstraps
Plugin Class Location
Place plugin classes in the Plugin/ directory of your module:
VendorName/ModuleName/Plugin/SomePlugin.php
Best Practices
- Prefer before/after over around — around is harder to debug
- Always call
$proceedin around plugins unless intentionally skipping - Use descriptive plugin names to avoid conflicts
- Set appropriate sortOrder when multiple plugins target the same method
- Check if a before or after plugin can achieve the goal before using around
- Test with other modules' plugins to verify sortOrder interactions
Fetch the plugins documentation for exact method signatures, the latest limitations list, and any changes in recent Magento versions before implementing.