How to Create Plugin in Magento 2

How to Create Plugin in Magento 2

As defined by the official document,

“A plugin or interceptor is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call and allows to substitute or extend the behavior of original, public methods for any class or interface.”

Simplifying the words,

“A plugin or interceptor is a way to insert code dynamically without changing the original class behavior. It allows extending the core functionality without any modification to the core files.”

Hopefully, for beginners, the term “plugin” is not confused with the “module” as they are the synonyms for platforms like WordPress. But not in Magento!

Magento 1 allowed to customize different classes and methods by rewriting a class. Powerful, but in this way, no modules could rewrite the same class, and hence, no flexibility. To overcome the rewrite conflicts and instability, Magento 2 comes with inceptors or plugins! And, the post is everything about it from the reasons to use, its restrictions, types and the method to create plugin in Magento 2.

Why should you use Inceptors or Plugins in Magento 2?

  • Minimum confliction among extensions that change the behavior of the same class or method
  • Avoid collisions between plugins by using sort order attribute of plugin
  • The plugin can be called sequentially according to sort order, so conflict with other plugin class can be avoided.
  • No issue of rewriting the system
  • Customize the same method in different modules
  • Ability to modify the return value of any method call that is used on an object manager controlled object
  • Ability to modify the arguments of any method call that is used on an object manager controlled object

However, it comes with limitations 🙁

The Magento 2 Plugins can’t be used with:

  • Non-public methods
  • Static methods
  • Final Methods
  • Final classes
  • Virtual Types
  • Any class that has at least one final public method
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • __construct

Types of Inceptors in Magento 2:

  1. Before listener

    1. Before listeners are used whenever we want to change the arguments of an original method or want to add some behavior before an original method is called.
    2. First methods to run in an observed method
    3. Before listener is called by adding the prefix ‘before’ to the method name and setting the first letter of original method to capital.
    4. It lets you modify the parameters that will be used.
    5. Syntax: beforeMethodname()
  2. After listener

    1. After listeners are used whenever we want to change the arguments of an original method or want to add some behavior after an original method is called.
    2. Starts running after the observed method is finished
    3. After listener is called by adding the prefix ‘after’ to the method name and setting the first letter of original method to capital.
    4. It let you modify the output
    5. Responsible for editing the results of an observed method in the right way and must have a return value.
    6. Syntax: afterMethodname()
  3. Around listener

    1. Inceptors run before and after the observed method.
    2. Allows overriding a method
    3. Used when you want to change both the arguments and the returned values of the observed method or add behavior before and after the observed method is called.
    4. Around listener is called by adding the prefix ‘around’ to the method name and setting the first letter of original method to capital.
    5. It must have a return value
    6. Syntax: aroundMethodname()

Steps to Create Plugin in Magento 2:

  1. Create registration.php file in app\code\[Vendor]\[Namespace]\
  2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc
  3. Create di.xml file in app\code\[Vendor]\[Namespace]\etc
  4. Create RestrictProductQuantity.php in [Vendor]\[Namespace]\Plugin\Checkout\Controller

Isn’t inceptor really a cool thing!

I’ve used this solution to configure TinyMCE4 toolbar in Magento 2.3.x versions.

With the above method, create plugin in Magento 2 easily.

Do not hesitate to use the Comments section below if you have any doubts with Magento 2 interceptors.

Thanks!

 

5
(based on 7 Reviews)

Sanjay is a co-founder at Meetanshi. He is a certified Magento developer who loves creating Magento E-commerce solutions. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a Reply