{"id":468,"date":"2019-06-20T10:30:04","date_gmt":"2019-06-20T10:30:04","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/20\/create-plugin-interceptor-in-magento-2\/"},"modified":"2025-05-22T16:33:39","modified_gmt":"2025-05-22T11:03:39","slug":"create-plugin-interceptor-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-plugin-interceptor-in-magento-2\/","title":{"rendered":"How to Create Plugin in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As defined by the&nbsp;<a href=\"https:\/\/developer.adobe.com\/commerce\/docs\/\" target=\"_blank\" rel=\"noreferrer noopener\">official document<\/a>,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u201cA 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&nbsp;<em>substitute<\/em>&nbsp;or&nbsp;<em>extend<\/em>&nbsp;the behavior of original, public methods for any&nbsp;<em>class<\/em>&nbsp;or&nbsp;<em>interface<\/em>.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simplifying the words,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u201cA 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.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hopefully, for beginners, the term \u201cplugin\u201d is not confused with the \u201cmodule\u201d as they are the synonyms for platforms like WordPress. But not in Magento!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;create plugin in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why should you use Inceptors or Plugins in Magento 2?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Minimum confliction among extensions that change the behavior of the same class or method<\/li>\n\n\n\n<li>Avoid collisions between plugins by using sort order attribute of plugin<\/li>\n\n\n\n<li>The plugin can be called sequentially according to sort order, so conflict with other plugin class can be avoided.<\/li>\n\n\n\n<li>No issue of rewriting the system<\/li>\n\n\n\n<li>Customize the same method in different modules<\/li>\n\n\n\n<li>Ability to modify the return value of any method call that is used on an object manager controlled object<\/li>\n\n\n\n<li>Ability to modify the arguments of any method call that is used on an object manager controlled object<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, it comes with limitations \ud83d\ude41<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Magento 2 Plugins can\u2019t be used with:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Non-public methods<\/li>\n\n\n\n<li>Static methods<\/li>\n\n\n\n<li>Final Methods<\/li>\n\n\n\n<li>Final classes<\/li>\n\n\n\n<li>Virtual Types<\/li>\n\n\n\n<li>Any class that has at least one final public method<\/li>\n\n\n\n<li>Objects that are instantiated before Magento\\Framework\\Interception is bootstrapped<\/li>\n\n\n\n<li>__construct<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Inceptors in Magento 2:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Before listener<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>First methods to run in an observed method<\/li>\n\n\n\n<li>Before listener is called by adding the prefix \u2018before\u2019 to the method name and setting the first letter of original method to capital.<\/li>\n\n\n\n<li>It lets you modify the parameters that will be used.<\/li>\n\n\n\n<li>Syntax:&nbsp;beforeMethodname()<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. After listener<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>Starts running after the observed method is finished<\/li>\n\n\n\n<li>After listener is called by adding the prefix \u2018after\u2019 to the method name and setting the first letter of original method to capital.<\/li>\n\n\n\n<li>It let you modify the output<\/li>\n\n\n\n<li>Responsible for editing the results of an observed method in the right way and must have a return value.<\/li>\n\n\n\n<li>Syntax:&nbsp;afterMethodname()<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Around listener<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inceptors run before and after the observed method.<\/li>\n\n\n\n<li>Allows overriding a method<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>Around listener is called by adding the prefix \u2018around\u2019 to the method name and setting the first letter of original method to capital.<\/li>\n\n\n\n<li>It must have a return value<\/li>\n\n\n\n<li>Syntax:&nbsp;aroundMethodname()<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Create Plugin in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n        \\Magento\\Framework\\Component\\ComponentRegistrar::register(\n            \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n            '[Vendor]_[Namespace]',\n            __DIR__\n        );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create<strong>&nbsp;module.xm<\/strong>l file in&nbsp;<em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n        &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>di.xml<\/strong>&nbsp;file in&nbsp;<em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n        &lt;type name=\"Magento\\Checkout\\Controller\\Index\\Index\">\n            &lt;plugin name=\"restrictCartQuantity\" type=\"[Vendor]\\[Namespace]\\Plugin\\Checkout\\Controller\\RestrictProductQuantity\"\/>\n        &lt;\/type>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>RestrictProductQuantity.php<\/strong>&nbsp;in&nbsp;<em><strong>[Vendor]\\[Namespace]\\Plugin\\Checkout\\Controller<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n    namespace [Vendor][Namespace]\\Plugin\\Checkout\\Controller;\n\n    class RestrictProductQuantity\n    {\n        public function aroundExecute(\n            Index $subject,\n            \\Closure $proceed\n        )\n        {\n            \/\/You required logic\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Isn\u2019t inceptor really a cool thing!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ve used this solution to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/configure-tinymce4-toolbar-in-magento-2-3-x\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em><strong>configure TinyMCE4 toolbar in Magento 2.3.x<\/strong><\/em><\/a>&nbsp;versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the above method,&nbsp;<em><strong>create plugin in Magento 2<\/strong><\/em>&nbsp;easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As defined by the&nbsp;official document, \u201cA plugin or interceptor is a class that modifies the behavior of public class functions by intercepting a function call&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-468","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":15375,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions\/15375"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}