Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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
17 votes

Magento 2: what is the $data array constructor parameter?

The $data[] can be used to hold values passed from di.xml's arguments replacement mechanism, when \Magento\Catalog\Block\Product\ListProduct intantiated. In your case this could be populated by <...
Codrain Technolabs Pvt Ltd's user avatar
14 votes
Accepted

Magento 2: what is the $data array constructor parameter?

The $data can be used to populate data on your object since the constructor of \Magento\Framework\DataObject is this public function __construct(array $data = []) { $this->_data = $data; } or ...
Kristof at Fooman'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
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
5 votes
Accepted

Magento 2.1: <preference> doesn't work with block

Thanks fschmengler , I managed to solve this by using Plugin. Override/replace single method in block class: app/code/MySpace/MyModule/etc/di.xml <config xmlns:xsi="http://www.w3.org/2001/...
pekkaliu's user avatar
  • 181
5 votes

Magento 2.1: <preference> doesn't work with block

To replace a single method, use the around plugin and don't call the original method with $proceed()
Fabian Schmengler's user avatar
5 votes
Accepted

Magento2: plugin around method not working

You've mixed up two concepts here: preferences and plugins. Preferences are basically rewrites on the class level. In case you define a preference for your controller your preference class will have ...
Raphael at Digital Pianism's user avatar
4 votes

Magento 2: preference not working in admin panel

ConfigurablePriceResolver is virtual type both used by ConfigurableFinalPriceResolver and ConfigurableRegularPriceResolver. Therefore, to override it, you have to override the virtual type, not the ...
Rendy Eko Prastiyo's user avatar
3 votes
Accepted

How to extend creditmemo save action in magento 2

You should try with Plugin. The code below will work: etc/adminhtml/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
Khoa TruongDinh's user avatar
3 votes
Accepted

Magento 2: preference not working in admin panel

Solution: app/code/vender/module/etc/di.xml <?xml version="1.0" encoding="UTF-8"?> <!-- To change this license header, choose License Headers in Project Properties. To change this template ...
Bhavik's user avatar
  • 1,240
3 votes

How to overwrite \Magento\Framework\Config\File\ConfigFilePool

Another option would be to use a Magento 2 plugin, for example, in a etc/di.xml: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
Raphael at Digital Pianism's user avatar
3 votes
Accepted

Magento 2 preference not working for Magento\Checkout\Block\Onepage

Unless your also overriding the template in your module, you need to change the module name for your class, otherwise magento looks for the template file in your module instead of the core module. You ...
Aaron Allen's user avatar
  • 9,009
3 votes

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

In brief : Preference is used to specify the default implementation of an interface. Plugin ( Interceptor ) is used to extend the behavior of a public method of another class. In detail : ...
nikin's user avatar
  • 1,132
3 votes
Accepted

Magento 2: How to override Magento\Catalog\Block\Product\View constructor to inject \Magento\Eav\Model\Entity\Attribute\SetFactory

So after taking everyones suggestions and getting as far as being able to render the product detail page without any errors but the block addtocart.phtml would not appear at all. I ran into this ...
Noemi Quezada's user avatar
3 votes
Accepted

Magento 2 not respecting preference?

I think it is because the class you are trying to override via preferences is actually being used by the system via virtualtype. vendor/magento/module-paypal/etc/di.xml <virtualType name="Magento\...
nick.graziano's user avatar
3 votes
Accepted

is it possible to override core plugin method by custom preference method?

You can disable the Plugin with disabled="true". For example: <type name="Magento\Framework\App\Action\Action"> <plugin name="storeCheck" disabled="true" /> <plugin name="...
Khoa TruongDinh's user avatar
3 votes

Magento 2, preference for Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection does not work

It work for me. I override the Catalog\Model\ResourceModel\fulltext\Collection.php. Create a etc\di.xml file. write the code <virtualType name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\...
Shekhar Suman's user avatar
3 votes

Magento 2, preference for Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection does not work

The reason the override for Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection doesn't work is because of the virtual type override in vendor/magento/module-elasticsearch/etc/di.xml. In ...
ionpan's user avatar
  • 31
3 votes

Magento2 Around Plugin vs Preference. Which option is better?

In general I would rank each of the customization approaches in this order, best option being first: Ranking Patches Plugins / Interceptors Observers Preferences Why? Patches I wouldn't officially ...
sdaugherty's user avatar
  • 1,231
3 votes

Magento 2 : How to Override /vendor/magento/module-quote/Model/Quote/Address/Total/Grand.php

You can create di.xml in etc/di.xml or if you want it to be area specific you can put in etc/frontend/di.xml or which ever area you want to override. Example of global preference app/code/...
Vishwas Bhatnagar's user avatar
3 votes

How can i override abstract class protected function

Preferences don't work for abstract classes because the abstract classes are never instantiated. But you can use preferences for the classes that extend the Magento\Sales\Controller\Adminhtml\Order\...
Marius's user avatar
  • 198k
2 votes

Magento 2: controller preference not working after setup:di:compile

I may be wrong but I'm pretty sure it's because those controllers are admin controllers and thus the corresponding di.xml should not be global. I reckon you should move your di.xml from etc to etc/...
Raphael at Digital Pianism's user avatar
2 votes
Accepted

Overriding Block file is not working in Magento 2

I think you misunderstanding plugins and preferences. If you want to override an entire class you need to use preferences: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/...
Raphael at Digital Pianism's user avatar
2 votes

Magento 2 override Pricing\Price\ConfigurablePriceResolver how to solve?

There is an excellent answer to the plugin option of your questions here: how magento getting configurable product lowest price of associated products? You can see the setup of the module and an ...
Todd's user avatar
  • 145
2 votes
Accepted

How to override _registerJsPrice($price) function in Configurable.php Block in Magento2?

Step 1:- create di.xml file in Folder app/code/Webkul/Hello/etc/ <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:...
Kushal Dani's user avatar
  • 2,114
2 votes

Magento 2, preference for Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection does not work

Try following way: <virtualType name="VendorName\ModuleName\Model\ResourceModel\Fulltext\CollectionFactory" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory"> <arguments> ...
Sohel Rana's user avatar
  • 35.9k
2 votes
Accepted

Undo preference from third party module

You can have your own class that extends Order class and does not make any changes. Like this: <preference for="Censored\Censored\Model\Order" type="Vendor\Module\Model\Order"/> In your Vendor\...
Prachi Saxena's user avatar
2 votes

Magento 2 : How to override files from Magento core framework?

I strongly recommend not use Preferences, because there are other techniques that you could use to update the input or output of a function. For example, you could write a Before plugin to update the ...
Mr. Lewis's user avatar
  • 1,931
2 votes
Accepted

How to override already override class in magento 2

<preference for="Amasty\Shopby\Model\ResourceModel\Fulltext\Collection" type="Abc\Cde\Model\ResourceModel\Product\Collection" />
Marius's user avatar
  • 198k

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