{"id":209086,"date":"2019-11-22T15:01:03","date_gmt":"2019-11-22T15:01:03","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=209086"},"modified":"2026-04-07T12:46:44","modified_gmt":"2026-04-07T12:46:44","slug":"plugins-observers-and-preference-in-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/","title":{"rendered":"Plugins, Observers and Preference in Magento 2"},"content":{"rendered":"\n<p>In this blog we will be discussing about the comparison between <a href=\"https:\/\/store.webkul.com\/Magento-2.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Plugins<\/a>, Preference and Observers.<\/p>\n\n\n\n<p>Magento 2 provides multiple options to modify the code extensively. However, you need to understand its strengths and weaknesses to use them in the most appropriate situations.<\/p>\n\n\n\n<p>First we would be\ndiscussing the <strong>Observers:<\/strong><\/p>\n\n\n\n<p>Magento lets you trigger events in the code, which are handled by the Event Manager. It then executes mapped classes to make the required changes in the application flow.<\/p>\n\n\n\n<p>The main advantage of using <strong>Observers<\/strong> is<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li> The original code is not changed and thus the changes that you will do the code, will not do any harm to the original code that is by far executing in the app till now. <\/li>\n<\/ul>\n\n\n\n<p>\nBut on the other hand,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If we consider the performance of the Observer setup, each event handler<br>\tclass is initiated separately and thus there is a large number of<br>\tobjects created while executing the Observers for a particular<br>\tevent. Magento however itself has a lot of objects being created<br>\tat any particular time, so we can however stop worrying about the<br>\tsame.<br>\t<\/li>\n\n\n\n<li>Also, the event catching and execution totally depends on the original method<br>\tdispatching the events. You can only execute your custom code when<br>\tthe original method has dispatched appropriate events for you. So,<br>\tinstead of using the Observers where you need to, it\u2019s dependent<br>\ton where the original developer has actually dispatched events for<br>\tyou.<br><\/li>\n<\/ul>\n\n\n\n<p> <br> Now coming to the <strong>Preference in Magento2:<\/strong><\/p>\n\n\n\n<p>Using the Preference, we tell Magento <strong>Object Manager<\/strong> to inter-change one class to another. The <strong>Object Manager<\/strong>,checks for preference class entry for a class while creating its new object. <\/p>\n\n\n\n<p>In case, an entry is found while injecting, it will swap the object from the original class to the parent class.<\/p>\n\n\n\n<p>The major problem while doing this is this will actually create a parallel flow of the code for execution and the original code will never be executed. <\/p>\n\n\n\n<p>This is something that we must avoid as changing the flow of the code, mostly returns in logical errors or bugs at certain levels.<\/p>\n\n\n\n<p><strong>Plugin, the Hero:<\/strong><\/p>\n\n\n\n<p>While executing the code, the Object Manager actually combines all the plugin classes and execute them all at once. <\/p>\n\n\n\n<p>The all is managed by the Dependency Injection in Magento 2 and thus, the original class is replaced by its Interceptor class without making any changes to the original class.<\/p>\n\n\n\n<p>\n<br>\nMain Advantages of using Plugins are: \n<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\tWe can execute our code before\/after\/around any class without<br>\tdepending on the code written in the original class.<br>\t<\/li>\n\n\n\n<li>\tThe original flow of the class is never changed and thus, once the<br>\tplugins are executed, the original class stays intact and thus the<br>\toriginal flow of the application is never changed.<br>\t<\/li>\n\n\n\n<li>It even allows the developer to limit the class methods that are<br>\tcritical for the execution of the class to not being changed as the<br>\tPlugins only work with the <strong>public <\/strong>methods<br>\tof the class<br><\/li>\n<\/ul>\n\n\n\n<p>\nThe points that we need to keep in mind while creating the Plugins\nare:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\tIf not handled properly ,the around plugin will overwrite the<br>\toriginal class and thus can result in conflicts very quickly when<br>\tthere are dependent modules executing on the same class.<br>\t<\/li>\n\n\n\n<li>Plugins\tshould not be created in the same module a class is being executed in<br>\t<\/li>\n\n\n\n<li>Plugins will not work for:\n<ul class=\"wp-block-list\">\n<li><br>Final<br>\t\tmethods<br>\t\t<\/li>\n\n\n\n<li><br>Final<br>\t\tclasses<br>\t\t<\/li>\n\n\n\n<li><br>Non-public<br>\t\tmethods<br>\t\t<\/li>\n\n\n\n<li><br>Class<br>\t\tmethods (such as static methods)<br>\t\t<\/li>\n\n\n\n<li><br>__construct<br>\t\t<\/li>\n\n\n\n<li><br>Virtual<br>\t\ttypes<br>\t\t<\/li>\n\n\n\n<li><br>Objects that<br>\t\tare instantiated before Magento\\Framework\\Interception is<br>\t\tbootstrapped<br>\t<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>That\u2019s all for the quick comparison between the Plugins, Observer and the Preference in Magento2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be discussing about the comparison between Magento 2 Plugins, Preference and Observers. Magento 2 provides multiple options to modify the code extensively. However, you need to understand its strengths and weaknesses to use them in the most appropriate situations. First we would be discussing the Observers: Magento lets you trigger <a href=\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":212,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2626,7989,2070],"class_list":["post-209086","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-code","tag-developer","tag-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Plugins, Observers and Preference in Magento 2 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Plugins, Observers and Preference in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog we will be discussing about the comparison between Magento 2 Plugins, Preference and Observers. Magento 2 provides multiple options to modify the code extensively. However, you need to understand its strengths and weaknesses to use them in the most appropriate situations. First we would be discussing the Observers: Magento lets you trigger [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-22T15:01:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-07T12:46:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishal Verma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal Verma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\"},\"author\":{\"name\":\"Vishal Verma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/580b43e8509d5fa4f51575bfb8bed418\"},\"headline\":\"Plugins, Observers and Preference in Magento 2\",\"datePublished\":\"2019-11-22T15:01:03+00:00\",\"dateModified\":\"2026-04-07T12:46:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\"},\"wordCount\":602,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"code\",\"developer\",\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\",\"name\":\"Plugins, Observers and Preference in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-11-22T15:01:03+00:00\",\"dateModified\":\"2026-04-07T12:46:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Plugins, Observers and Preference in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/580b43e8509d5fa4f51575bfb8bed418\",\"name\":\"Vishal Verma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/31e9b57460b4925d92baea86384d39a23da9408a0d216caea290a9ae75e4b310?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/31e9b57460b4925d92baea86384d39a23da9408a0d216caea290a9ae75e4b310?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Vishal Verma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/vishalverma-magento279\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Plugins, Observers and Preference in Magento 2 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/","og_locale":"en_US","og_type":"article","og_title":"Plugins, Observers and Preference in Magento 2 - Webkul Blog","og_description":"In this blog we will be discussing about the comparison between Magento 2 Plugins, Preference and Observers. Magento 2 provides multiple options to modify the code extensively. However, you need to understand its strengths and weaknesses to use them in the most appropriate situations. First we would be discussing the Observers: Magento lets you trigger [...]","og_url":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-11-22T15:01:03+00:00","article_modified_time":"2026-04-07T12:46:44+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Vishal Verma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Vishal Verma","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/"},"author":{"name":"Vishal Verma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/580b43e8509d5fa4f51575bfb8bed418"},"headline":"Plugins, Observers and Preference in Magento 2","datePublished":"2019-11-22T15:01:03+00:00","dateModified":"2026-04-07T12:46:44+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/"},"wordCount":602,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["code","developer","Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/","url":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/","name":"Plugins, Observers and Preference in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-11-22T15:01:03+00:00","dateModified":"2026-04-07T12:46:44+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/plugins-observers-and-preference-in-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Plugins, Observers and Preference in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/580b43e8509d5fa4f51575bfb8bed418","name":"Vishal Verma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/31e9b57460b4925d92baea86384d39a23da9408a0d216caea290a9ae75e4b310?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/31e9b57460b4925d92baea86384d39a23da9408a0d216caea290a9ae75e4b310?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Vishal Verma"},"url":"https:\/\/webkul.com\/blog\/author\/vishalverma-magento279\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/209086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/212"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=209086"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/209086\/revisions"}],"predecessor-version":[{"id":534492,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/209086\/revisions\/534492"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=209086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=209086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=209086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}