Skip to main content
24 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.9k
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
4 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,142
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

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
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
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 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

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,029
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,250
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
  • 199k
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,134
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
  • 36.1k
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,951
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
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
  • 199k
2 votes

Magento 2 Override public static method of framework class

Overwriting a static method is impossible, because the interpreter call them using a class name directly, so no one preference could get around this logic. It is a PHP restriction, not Magento. You ...
Siarhey Uchukhlebau's user avatar
2 votes

Magento 2.3 - How to handle Preference Error - Type Error occurred when creating object

It's a bit hard to see exactly what has fixed this error since the initial code isn't available anymore (https://paste.ofcode.org/eggKeUuvGeE3izxV6K9rUC), but for me what fixed the issue was extending ...
Andrei Cristian's user avatar
2 votes

Override Magento\Sales\Model\Order\Email\Sender\CreditmemoSender Magento 2

With below code you can override CreditmemoSender.php file Step 1: Create di.xml file PackageName/Module/etc/ <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/...
ZealousWeb's user avatar
  • 2,138
2 votes
Accepted

How to override the Search.php using preference in magento 2?

You can only specify preferences for classes that get instantiated. From what I see, the class Magento\Framework\Search\Search does not get instantiated. It is only extended by the class Magento\...
Marius's user avatar
  • 199k
2 votes
Accepted

Preference Module - When to Pass in Parent Dependencies

dependencies and constructors are conditional if you're referring to preference/overriding class. if you're using another dependencies which are not in parent class, you need to declare __construct ...
Fiko's user avatar
  • 106
1 vote

Private method error while overriding Magento\Customer\Model\ResourceModel\CustomerRepository

=> In di.xml of your custom module <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/...
Learing_Coder's user avatar
1 vote

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

The reason why it doesn’t work is because the class Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection is used in the CatalogSearch with customization through virtual types and there is ...
Nilesh Gosai's user avatar
  • 1,155
1 vote
Accepted

Overriding Custom module's controller in Magento2 giving error

Repalce your controller constructer like this. public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory, OrderFactory $modelOrderFactory) { ...
Dhiren Vasoya's user avatar

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