magento 2 plugin

 

Magento 2 Plugins are the technical plugins for better writing code. Interception Plugins are referred to as small extensions of Magento 2 that allow changing or editing the behavior of any public class or method by intercepting a function call and running code either before or after or around the class while there is no need to change the actual class directly. 

Benefits Of Magento 2 Plugins

The following are the core benefits of using Magento 2 Plugins

  1. Smooth delivery of the products to customers is the next step after setting up the online eCommerce store. That is why using plugins help you to manage delivery date and time of every single order. 
  2. If you want to enhance your business overseas,then you might face language issues and more but the Advanced GeoIP Redirect plugin resolves all the barriers and will help you to expand your business globally with wider audience reach. 
  3. Before Listener and After Listener plugins ensure hassle-free order management. 
  4. You can consider the plugins as an engagement booster because they help you to reach more targeted peoples. 
  5. Facebook feed Magento 2 extension helps you to increase engagement in your social media pages too.

Plugin’s Restrictions

What options doesn’t Magento 2 Interception plugin work with?

  • Objects that are instantiated before Magento\Framework\Interception is reboot
  • Final methods
  • Final classes
  • Any class that contains at least one actual public method
  • Non-public methods
  • Class methods 
  • __construct
  • Virtual types

How To Create Magento 2 Plugin

The following is a step by step process of creating Magento 2 Plugin 

Step 1: State A Plugin In Magento 2

File path: app/code/MageCaptain/Tutorial/etc/di.xml



<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd'>
   <type name='MageCaptain\Tutorial\Controller\Index\Example'> 
     <plugin name='MageCaptain_Tutorial_Plugin' type='MageCaptain\Tutorial\Plugin\ExamplePlugin' sortOrder='10' disabled='false'  />
   </type>
</config>


Step 1: State A Plugin In Magento 2

File path: app/code/MageCaptain/Tutorial/Controller/Index/Example.php


<?php 
namespace MageCaptain\Tutorial\Controller\Index;
class Example extends \Magento\Framework\App\Action\Action {   
    protected $title;   
    public function execute() { 
      echo $this->setTitle('Welcome'); 
      echo $this->getTitle(); 
    }   

    public function setTitle($title) { 
      return $this->title = $title; 
    }   

    public function getTitle() { 
      return $this->title; 
    } 
}

Step 3: Defining A Plugin

A plugin is a great way to enhance or edit an actual method’s behavior by using code before, after or around the plugin. Firstly, get an object that provides permission to all actual methods of the observed method’s class.

 

3 types of Plugin

  • before - beforeDispatch()
  • around - aroundDispatch()
  • after - afterDispatch()

Let’s discuss the exact coding method of all three types of Magento 2 Plugins

Before Listener Plugin 

Before methods are the very first plugin method to run in an observed method, and these plugins must have the exact name to the observed one’s name while the prefix label is before. For the application of the before listener plugin method you need to modify the arguments of an observed method, you can return a new modified argument. If there are various arguments, the returning will be carried out according to a range of those arguments. Moreover, if the return is wrong or invalid, which directly means the arguments for the observed method should not be changed. 

File path: app/code/MageCaptain/Tutorial/Controller/Index/Example.php



<?php     
namespace MageCaptain\Tutorial\Plugin;   
class ExamplePlugin {

    public function beforeSetTitle(\MageCaptain\Tutorial\Controller\Index\Example $subject, $title) {

       $title = $title . ' to ';

       echo __METHOD__ . '</br>';

       return [$title]; 

    }   

}

After Listener Plugin

The After Listener plugin methods start running right after the observed method is finished, and these methods must have the same name as the actual one’s name while the prefix label is ‘after’. The After listener plugin takes the responsibility of editing the results of an actual method in the correct way and being required to have a return value.

File path: app/code/MageCptain/Tutorial/Controller/Index/Example.php


<?php     

namespace MageCaptain\Tutorial\Plugin;   

class ExamplePlugin {   
    public function afterGetTitle(\MageCaptain\Tutorial\Controller\Index\Example $subject, $result) {   
      echo __METHOD__ . '</br>';   
      return '<h1>'. $result . 'MageCaptain.com' .'</h1>';   
    }   
}

Around Listener Plugin 

The Around listener plugin method allows the code to run before and after the observed method, so you can override a plugin. These methods must have the exact name as the actual one’s name while the prefix label is ‘around’. Before arranging the original method’s argument, a callable from around methods will be called to the next method in the chain, which means the next plugin or the actual function is also called. 

Always remember: If the case of callable is not declared, the calling to neither the next plugin nor the actual method is achieved.

File path: app/code/MageCaptain/Tutorial/Controller/Index/Example.php


<?php     

namespace MageCaptain\Tutorial\Plugin;   

class ExamplePlugin {

    public function aroundGetTitle(\Mageaptain\Tutorial\Controller\Index\Example $subject, callable $proceed) {   
        echo __METHOD__ . ' - Before proceed() </br>'; 
        $result = $proceed(); echo __METHOD__ . ' - After proceed() </br>'; 

        return $result; 
    }   
}

Conclusion

Magento 2 updated plugins are very useful for the users, and if you want to hire a certified Magento 2 agency then MageCaptain is one of the best places to choose.