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\...
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/...
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()
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 :
...
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 ...
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 ...
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="...
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\...
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:...
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 ...
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:...
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\...
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 ...
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 ...
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/...
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\...
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 ...
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:...
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>
...
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 ...
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\...
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" />
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 ...
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 ...
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/...
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\...
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 ...
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/...
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 ...
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)
{
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
preference × 103magento2 × 79
overrides × 30
plugin × 19
dependency-injection × 9
magento2.3 × 8
di × 8
module × 7
magento-2.1 × 6
override-model × 5
magento2.2 × 4
di.xml × 4
cart × 3
configurable-product × 3
error × 3
controllers × 3
magento2.4 × 3
model × 3
extend × 3
product × 2
php × 2
checkout × 2
event-observer × 2
blocks × 2
api × 2