How to Create Preference in Magento 2?

Preferences in Magento 2

In Magento 2, Preferences are used to overwrite the whole (core)file or folder. We can use the preference to override or rewrite the block, model, helper, controller. We can also rewrite custom module files. If we want to override a public or protected method from a core class, we can utilize the preference from di.xml to achieve it. Preference method is a powerful way to configure Magento’s core functionality.

Step 1 : Create all the commonly needed files:

Refer this blog and create module.xml file inside etc folder and registration.php file inside Code5fixer/PreferenceDemo folder. Here, PreferenceDemo is the name of the module folder.

i.e.,

app/code/Code5fixer/PreferenceDemo/etc/module.xml
app/code/Code5fixer/PreferenceDemo/registration.php

Step 2: Create di.xml to define preference

We define preference into app/code/VendoreName/ModuleName/etc/di.xml

app/code/Code5fixer/PreferenceDemo/etc/di.xml

The contents of di.xml file will be:

di.xml

<?xml version="1.0"?>
    <config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Product" type="Code5fixer\PreferenceDemo\Model\Product" />
</config>
                

We use attribute “for” in the “preference” tag to define the PHP class that we want to override. Then we use attribute “type” to define the PHP class that will be used instead of the original one.

Step 3: Create the php file

Now, it is time to create a php file that should overwrite Magento\Catalog\Model\Product class. To do that create a folder inside PreferenceDemo folder and name it as Model. Inside Model folder, create a php file and name it as Product.php

app/code/Code5fixer/PreferenceDemo/Model/Product.php

The contents of Product.php file will be:

Product.php

<?php
 namespace Code5fixer\PreferenceDemo\Model;
 class Product extends \Magento\Catalog\Model\Product
{
    public function getName()
    {
        return "C5F Magento Course";
    }
}
                

We specify getName() fuction to return C5F Magento Course, which means all the product names will be changed to C5F Magento Course in the frontend.

Step 4 : Run all the necessary commands

We need to run commands like setup:upgrade, static-content:deploy, setup:di:compile, reindex and cache:flush from the Magento root directory.

To know more about more magento commands, refer this blog.

Refreshing our home page, we could see that the names of all the products have been changed to “C5F Magento Course” as shown below:

Both Plugins and Preferences are helpful in overriding the classes. However, Plugins are preferable than Preferences since plugins do not override the class logically instead it hooks our logic into the available classes. So, to modify or extend any existing business logic, it is better to use the plugins. Preferences are not recommended unless or otherwise there is no other option.

You could also download the above module here

After downloading this module:

  1. Extract the downloaded zip file
  2. Paste the extracted Code5fixer folder into /app/code/ folder in your magento root directory.
  3. Run all the necessary 5 commands and check for output.
This module is developed using Magento ver. 2.4.3-p1 version. But if you try this in older or newer versions, it may give some errors.

You could also refer to this demo video for better understanding.

We hope our guide would be very useful for you. If you have any questions, feel free to reach us anytime by leaving a comment below.

Check out our new blogs here

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments