Loading...
Unlock The Power Of Plugins In Magento 2: A Comprehensive Guide

Unlock The Power Of Plugins In Magento 2: A Comprehensive Guide

Introduction

Plugins in Magento 2 are like special tools that can modify how things work without changing the original code. They act as an extra layer and intercept the behaviour of existing code, allowing you to customize and extend the functionality of your Magento 2 store modularly.

Benefits of Using Plugins

Using plugins in Magento 2 comes with several advantages. They help keep your code organized by separating your customizations from the core code. Using plugins in Magento 2 has two main benefits.

Firstly, when you need to upgrade your store, plugins make it easier to keep your modifications intact without losing them. 

Secondly, plugins help avoid conflicts with other customizations, ensuring that your store's different parts work well together without any issues. In simple terms, plugins provide a smooth and hassle-free way to upgrade and customize your Magento 2 store.

Overall, plugins offer a flexible and reliable way to enhance your Magento 2 store's functionality while keeping the core code intact.

The ability of Plugins in Magento 2

Here is a list of the Ability of Plugins in Magento 2.

  • Here are three types of plugins: around, before and after.
  • Plugins only work on public methods.
  • Plugins do not work on final methods or final classes.
  • You can utilize plugins on interfaces, abstract classes, or parent classes.
  • Plugins are mainly used for the solution of Module/Class override conflict.

– Before plugins, modify the input arguments to a method. You can change them to any value.

Example from: \Magento\Catalog\Block\Product\ListProduct

php

<?php
 public function prepareSortableFieldsByCategory($category)
{
     // receive the category as argument and process it.. 
      return $this; // return the 
}
?>

To modify the argument of the prepareSortableFieldsByCategory($category) method, add a method to the plugin class:

We need to define the method name with the prefix "before" for this.

The method above is run before "\Magento\Catalog\Block\Product\ ListProduct::prepareSortableFieldsByCategory".

The return value for the before plugin determines the arguments going into the following plugin or the final targeted method.

php

<?php 
 public function beforePrepareSortableFieldsByCategory( \Magento\Catalog\Block\Product\ListProduct $context,  $category ) 
 {
     // ...made changes here with category passed by refrenes...
     return [$category]; 
 }
?>

– After plugins are used to modify the return value.
Example from: \Magento\Catalog\Block\Product\ListProduct

php

<?php
 public function getProductPrice(Product $product)
{
    // receive the product as argument and process it.. 
     return $price; // return the 
}
?>

If you need to modify the output from a public method, use an after-plugin.

Let's modify the getProductPrice($product) in our example class. As such, in our plugin class, we would create the following:

php

<?php
 public function afterGetProductPrice( \Magento\Catalog\Block\Product\ListProduct $context,   $result,   \Magento\Catalog\Model\Product $product ) 
 {
     // ...made changes here with category passed by refrenes...
     return $result; 
 }
?>

The after plugin includes the input parameters in addition to the return result.

– The around plugin modification of the input and output of a function. The original function is passed in as a callback and, by standard, is named "$proceed".

Example from: \Magento\Catalog\Block\Product\ListProduct

php

<?php
 public function getProductPrice(Product $product)
{
    // receive the product as argument and process it.. 
     return $price; // return the 
}
?>

Let's modify the getProductPrice($product) in our example class. As such, in our plugin class, we would create the following:

php

<?php 
public function aroundGetProductPrice( \Magento\Catalog\Block\Product\ListProduct $context, callable $proceed)
   {
       echo __METHOD__ . " - Before original method call </br>";
       
       $result = $proceed();
       
       echo __METHOD__ . " - After original method call </br>";
       
       return $result;
   }
?>

We must define the method name with the prefix "around" for this.

We should return the "Original methods returned value" to execute the following plugin.

If the callable is not declared, neither the following plugin nor the original method can be called.

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

FAQs about Plugins In Magento 2

How does Plugin work?

How do multiple plugins interact, and how can their execution order be controlled?

In which cases should plugins be avoided?

Pravin Prajapati
Full Stack Developer

Expert in frontend and backend development, combining creativity with sharp technical knowledge. Passionate about keeping up with industry trends, he implements cutting-edge technologies, showcasing strong problem-solving skills and attention to detail in crafting innovative solutions.

Most Visited Blog

Blog
Are you looking to migrate from Magento 1 to Magento 2?

Think of a seamless transition! Explore the steps and benefits of migrating from Magento 1 to Magento 2. Ensure a seamless upgrading procedure in e-commerce platform for improved performance and advanced features.

Read More
Blog
How to Integrate a Custom Reducer into Your Website?

Effortlessly integrate a custom reducer into your website with our quick guide. Enhance your Magento PWA shop by introducing custom reducers for improved state management and user experience.

Read More