The Power of Around Plugins

Experience how Around plugins wrap and control Magento methods

Core Method
save()
$subject
$proceed()
return $result
aroundSave()
public function aroundSave($subject, $proceed) {
    // Pre-execution logic
    $result = $proceed(); // Wrapped!
    // Post-execution logic
}

Listen to Article

How to Use Around Plugin Magento 2 in E-Stores?

00:00
0:00 --:--

Which Plugin Type Should You Use?

Answer these questions to find the perfect plugin type for your needs

Before Plugin

Modify input parameters before execution

Around Plugin

Complete control over method execution

After Plugin

Transform return values after execution

Implement Your Around Plugin

Follow these 9 steps to create a working Around plugin in Magento 2

Steps

Step 1 of 9

Performance Impact Analysis

Visualize how Around plugins affect your Magento 2 call stack and execution time

+0
Stack Depth
0ms
Execution Time
0KB
Memory Usage

Without Plugin

1. Controller Action
ProductController->saveAction()
2. Model Layer
Product->save()
3. Resource Model
ResourceModel->save()
Total: 3 calls ~25ms

With Around Plugin

1. Controller Action
ProductController->saveAction()
2. Plugin Interceptor
Interceptor->save()
3. Around Plugin
Plugin->aroundSave()
4. Model Layer
$proceed() -> Product->save()
5. Resource Model
ResourceModel->save()
Total: 5 calls ~40ms

Performance Considerations

Each Around plugin adds 2+ calls to the stack
Multiple plugins multiply the overhead
Deep nesting complicates debugging
Memory usage increases with depth

When NOT to Use Around Plugin

Interactive checklist to help you avoid common pitfalls and find better alternatives

Critical - Never use
Warning - Avoid if possible
Caution - Consider alternatives

When You Need to Change Input Parameters CRITICAL

If you only need to modify input values, an around plugin adds unnecessary complexity

When Performance Is a Priority CRITICAL

Around plugins significantly increase call stack depth and execution time

When You're Unsure About proceed() Logic WARNING

Incorrect $proceed() usage can break core functionality silently

When Plugin Conflicts Are Likely WARNING

Multiple around plugins on the same method create complex chains

When Code Maintainability Is a Priority CAUTION

Around plugins make code flow harder to understand for new developers

Interactive Code Examples

Explore real Around plugin implementations with interactive annotations

Show Annotations
namespace Vendor\Module\Plugin;

class ProductAttributesUpdater
{
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    public function aroundSave(
        \Magento\Catalog\Model\Product $subject,
        callable $proceed
    ) {
        // Pre-execution: Log before save
        $this->logger->info('Before save: ' . $subject->getName());
        
        // Call original method
        $result = $proceed();
        
        // Post-execution: Log after save
        $this->logger->info('After save: ' . $subject->getId());
        
        // Always return the result
        return $result;
    }
}
1
Pre-execution
2
Original method
3
Post-execution

Interactive Tips

  • Hover over code elements to see detailed explanations
  • Click "Show Annotations" to reveal inline documentation
  • Toggle between different views in the real-world example
  • Use these patterns as templates for your own implementations

Plugin Type Comparison

Interactive comparison of Around, Before, and After plugins in Magento 2

Criteria Around Plugin Before Plugin After Plugin
Execution Control
Ability to control method execution
Full Control
Can skip or modify execution
No Control
Method always executes
No Control
Method always executes
Performance Impact
Effect on execution speed
High
+2 call stack, ~60% slower
Low
Minimal overhead
Low
Minimal overhead
Implementation Complexity
Difficulty to implement correctly
Complex
Requires proceed() management
Simple
Straightforward implementation
Simple
Straightforward implementation
Best Use Cases
When to use each type
  • Complex control flow
  • Conditional execution
  • Both pre/post logic needed
  • Input modification
  • Parameter validation
  • Pre-processing data
  • Output transformation
  • Result formatting
  • Post-processing data
Debug Difficulty
How hard to debug issues
Hard
Complex stack traces
Easy
Clear execution path
Easy
Clear execution path

Quick Decision Guide

1

Need to modify inputs? Use Before Plugin

2

Need to modify outputs? Use After Plugin

3

Need conditional execution or both? Use Around Plugin

Best Practices Dashboard

Track your Around plugin implementation with this interactive checklist

0%
Complete
0/15
Tasks Done
3
Warnings
--
Score

Planning & Design

Implementation

Testing & Documentation

Performance & Monitoring

Quick Tips

Always call $proceed() exactly once per execution path

Never use Around plugins in performance-critical loops

Lower sortOrder values execute first in the plugin chain

Your Progress

Starter
Pro
Expert