Magento 2 Preferences

Loading...
Magento 2 Preferences

Magento 2 Preferences

Preference is used by the Object Manager to indicate the default implementation. You can use it to rewrite a class from another module to point at the implementation, which will cause the class to be used globally. If you want to override a public or protected method from a core class, utilize the preference from di.xml to achieve it. The preference node specifies the default implementation.

We can use the preference to override or rewrite the block, model, helper, controller. We can also rewrite custom module files. We define preference into app/code/VendoreName/ModuleName/etc/di.xml

Example of overriding a method from a core file. We rewrite the getName() function of class Magento\Catalog\Model\Product.

app/code/YVendor/NewMod/etc/di.xml





Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.