68
votes
Accepted
Magento2: what is the basic difference between plugin and preference?
A preference is equivalent to class rewriting from Magento 1. It's equivalent to saying, "Whenever code asks for ClassA, give them MyClassB instead." MyClassB is expected to be a complete ...
45
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 ...
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)
...
35
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 ...
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 ...
33
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" ...
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\...
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 ...
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 ...
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.
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
...
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 $...
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\...
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 ...
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 ...
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 ...
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/...
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 ...
11
votes
What Classes are "Intercept-able/Plugin-able" in Magento 2
We are working on "@api" annotations to annotate recommended methods that will be more stable across releases. If you worry about upgradability, in additional to what can have a plugin defined, you ...
11
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 "...
10
votes
Accepted
What Classes are "Intercept-able/Plugin-able" in Magento 2
Every Class of an Magento Module is intercaptable.
As described on the current wiki, it is limited by final methods and classes
Not validated, but Classes of libraries (lib directory) are(/should) ...
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
...
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}"&...
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 ...
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/...
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\...
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 ...
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:...
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 ...
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
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
plugin × 612magento2 × 452
event-observer × 61
magento-2.1 × 41
overrides × 35
magento2.3 × 34
module × 34
after-plugin × 24
magento2.2 × 22
checkout × 20
interceptor × 20
preference × 19
magento2.4 × 18
before-plugin × 18
product × 17
extensions × 17
di × 17
magento-1.9 × 16
php × 16
shipping-methods × 15
model × 13
layout × 12
orders × 12
customer × 11
error × 11