Hot answers tagged

46 votes
Accepted

Magento 2: Plugin vs Observer

Plugins are omnipresent since it is possible to modify/replace the behavior of any public method in the system. Customizations should be done using plugins for public methods/classes marked with @api ...
Alex Paliarush's user avatar
39 votes
Accepted

Magento 2: Plugin before/around/after Interaction

Plugins are sorted by sort order first, and then by method prefix. Example: for method with 3 plugins (PluginA, PluginB, PluginC) with following methods and sortOrder: PluginA (sortOrder = 10) ...
Anton Kril's user avatar
  • 4,486
36 votes
Accepted

Magento 2: what are the Interceptors file in var/generation?

Interceptor classes are an implementation of the interceptor design pattern. The interceptors are how how Magento 2's object system implements a plugin system. As a client developer, the theory is ...
Alana Storm's user avatar
  • 44.3k
34 votes
Accepted

How to override or disable core Plugins in Magento2?

You can disable plugin by name. In your case it authorization. <type name="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization"> <plugin name="authorization" ...
sergei.sss's user avatar
  • 1,169
33 votes

Is it possible to disable plugins defined in di.xml programatically?

In your di.xml you can do the following: <type name="\Magento\Catalog\Model\Product"> <plugin name="se_catalog_product_plugin_ample" disabled="true" /> </type> Magento does it ...
Raphael at Digital Pianism's user avatar
23 votes

Magento2: what is the basic difference between plugin and preference?

In simple words Preference is used for overriding class Plugin is used for adding functionality before, after and around methods. For As Your example: <preference for="Magento\Catalog\...
Prince Patel's user avatar
  • 22.7k
20 votes
Accepted

Rewriting Magento 2 classes vs Plugins

The obvious reason to use a rewrite instead of a plugin is when you need to override a private, protected or final methods. But also consider the following scenarios. 1st scenario (absolute sort ...
Phoenix128_RiccardoT's user avatar
20 votes
Accepted

Plugins for magic getters/setters

Is that even possible? Yes. How can I pluginize the magic getters/setters in magento 2? In the same way as a other public method. You need to declare plugin in di.xml configuration and add you ...
Max's user avatar
  • 4,044
16 votes

Is dependency injection possible for plugins?

Late answer but you need to change __constructor to __construct and it will work perfectly well. Don't use static ObjectManager if possible. You should always use DI instead.
Luke's user avatar
  • 772
16 votes

Magento 2: Plugin before/around/after Interaction

From the Magento 2 cookbook: If there are multiple plugins that extend the same original function, they are executed in the following sequence: the before plugin with the lowest sortOrder ...
Raphael at Digital Pianism's user avatar
16 votes

Magento 2 - Get Request Params / Api Request Params From Plugin

Looks like you are using wrong class in your construct for $request. Change your construct to following and it should work: public function __construct( \Magento\Framework\Webapi\Rest\Request $...
Reena Parekh's user avatar
  • 1,917
14 votes

How to override or disable core Plugins in Magento2?

If you use sergei.sss 1st solution, you will get the error of duplicate Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization The correct way to do that is: <type name="Magento\Sales\Model\...
fudu's user avatar
  • 1,268
13 votes

Magento 2: virtual types plugins

No, plugins on virtual types do not work. Proof of concept code: <?php namespace Training\Example\Model class Type { public function bar() { return __CLASS__; } } Plugin ...
Vinai's user avatar
  • 14k
13 votes
Accepted

Magento 2 Plugin implementation for js frontend?

You do this with using a requirejs mixin and the mage/utils/wrapper js module. Heres a demonstration of how the wrapper module functions: var wrapper = require('mage/utils/wrapper'); function ...
Aaron Allen's user avatar
  • 9,009
12 votes

Override abstract class in Magento 2

Plugins only? Yes. You can write plugins for abstract classes and plugins should always be preferred over preferences if possible. Preferences are useful if you want to replace an implementation. I ...
Fabian Schmengler's user avatar
12 votes

Magento 2: get a list of all plugins (interceptors) and preferences

For getting all available list of plugins: Go to vendor/magento/framework/Interception/PluginList/PluginList.php find method merge public function merge(array $config) { // default code echo "...
Yogesh Karodiya's user avatar
12 votes
Accepted

Magento2 plugin around method different between callable and Closure paramters on param $proceed

You have an answer here : https://stackoverflow.com/a/29730810/4629261 The difference is, that a [Closure] must be an anonymous function, where [callable] also can be a normal function. You can see/...
Vinz's user avatar
  • 985
11 votes
Accepted

Magento 2: Which extension mechanism to choose?

Use plugins whenever possible, then observers (see the pros and cons of each), this will allow you to avoid conflicts with other extensions (3rd party modules). Preference should be used only if the ...
Alex Paliarush's user avatar
10 votes
Accepted

Get Store View id or code in plugin in magento2

I think you misread the documentation. Basically the following are plugins limitations: Objects that are instantiated before Magento\Framework\Interception is bootstrapped Final methods ...
Raphael at Digital Pianism's user avatar
9 votes
Accepted

How can I disable a plugin in a module from another module?

Edit: just saw that you already tried the solution below. AFAIK it's the only "clean" way of doing it You can add the following in your own di.xml <config> <type name="{ObservedType}"&...
Raphael at Digital Pianism's user avatar
9 votes

What are the top useful Phpstorm plugins for Magento 2 development?

1) PHPStorm with XDebug: PHPStorm is my favorite IDE. It is an advanced IDE for PHP developers. Many developers still use only var_dump and die to debug the system. From my experience, these ...
Khoa TruongDinh's user avatar
8 votes

Override abstract class in Magento 2

If abstract class have any public or protected method you want to override, there is a way actually if you can't use plugins. I had to override method _processDownload inside \Magento\Downloadable\...
Bartosz Kubicki's user avatar
8 votes

Override abstract class in Magento 2

Full solution: include replaced classes before magento autoload them. So step by step: In file app/etc/NonComposerComponentRegistration.php add row $pathList[] = dirname(__DIR__) . '/etc/...
goral's user avatar
  • 659
8 votes

Rewriting Magento 2 classes vs Plugins

Great question, I asked myself the same thing the other day and here's what I came up with: First, plugins cannot be used for final methods, final classes and classes created without dependency ...
Raphael at Digital Pianism's user avatar
8 votes
Accepted

Magento 2 Save customer attribute value while customer register

You can do this by observer. SR/StackExchange/etc/frontend/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:...
Sohel Rana's user avatar
  • 35.7k
8 votes
Accepted

Magento 2 javascript plugin after method is executed in wrong time

If you look at target file Magento/Checkout/view/frontend/web/js/view/shipping.js,you'll see that this is requirejs module which returns UiComponent (child of Magento_Ui/js/form/form). In your ...
Max's user avatar
  • 4,044
8 votes
Accepted

Magento2 - plugin / event after invoice is created

I finally solved this using sales_order_invoice_pay event, which is fired both in admin & frontend scopes
Raul Sanchez's user avatar
  • 3,276
8 votes
Accepted

Magento 2.2.4 Show error message returned by plugin

After so much investigation and implementation, I found a solution to display custom error message on checkout page. 1) Override a shipping.js file (vendor\magento\module-checkout\view\frontend\web\...
Dhara Bhatti's user avatar
8 votes
Accepted

Magento 2 :How to change or update request data using beforeExecute plugin?

Try the following way: $subject->getRequest()->setParam('existing_key', $newValue);
Sohel Rana's user avatar
  • 35.7k
7 votes
Accepted

Magento 2, override \lib\ classes

It's definitely feasible using preferences. You can do something like this in a di.xml file: <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:...
Raphael at Digital Pianism's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible