---
title: "How to Create Preference in Magento 2"
url: "https://dolphinwebsolution.com/how-to-create-preference-in-magento-2"
date: "2021-03-23T05:59:38+05:30"
modified: "2023-08-31T05:11:05+05:30"
author:
  name: "Mahesh Makwana"
categories:
  - "Magento"
word_count: 299
reading_time: "2 min read"
summary: "Today we learn how to use Preference in Magento 2. At development time we need to rewrite some files, Preference helps us to achieve this.
Preference is used by the Object Manager to indicate the ..."
description: "Today we learn how to create Preference in Magento 2. At development time we need to rewrite some files, Preference helps us to achieve this."
keywords: "how to create Preference in Magento 2, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How To Implement Field Dependency  In System Configuration Magento 2"
    url: "https://dolphinwebsolution.com/how-to-implement-field-dependency-in-system-configuration-magento-2"
  - title: "Magento Community vs. Magento Enterprise: Which One is Right for Your Business?"
    url: "https://dolphinwebsolution.com/blog/magento-community-vs-magento-enterprise/"
  - title: "How to Add Customer Attribute Programmatically in Magento 2"
    url: "https://dolphinwebsolution.com/how-to-add-customer-attribute-in-magento-2"
---

# How to Create Preference in Magento 2

_Published: March 23, 2021_  
_Author: Mahesh Makwana_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/Preference-in-Magento-2-1024x512.png)

Today we learn how to use **Preference** in Magento 2. At development time we need to rewrite some files, Preference helps us to achieve this.

**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()**](https://magento.stackexchange.com/questions/120366/cant-getname-from-categorycollection-while-getid-and-geturl-work) function of class **Magento\\Catalog\\Model\\Product**.

**app/code/Dolphin/PreferenceDemo/etc/di.xml**

```
<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="Dolphin\PreferenceDemo\Model\Product" />
</config>
```

**app/code/Dolphin/PreferenceDemo/Model/Product.php**

```
<?php

namespace Dolphin\PreferenceDemo\Model;

class Product extends \Magento\Catalog\Model\Product
{
    public function getName()
    {
        return "Preference Demo";
    }
}
```

**Output: All product names are “Preference Demo” in the frontend.**

![Preference in magento 2](https://dolphinwebsolution.com/wp-content/uploads/2021/03/Screenshot-from-2021-03-23-11-26-30.jpg)

Preference method is a powerful way to configure Magento’s core functionality. Sometimes, it might be too heavy-handed. The disadvantage of using preferences is that it can cause conflicts if multiple classes extend the same original class. Under some circumstances, you may want Magento’s default class selection to remain in place.

Preference is commonly used in [Magento 2 development](https://dolphinwebsolution.com/magento-2-development/).

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.


---

_View the original post at: [https://dolphinwebsolution.com/how-to-create-preference-in-magento-2](https://dolphinwebsolution.com/how-to-create-preference-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 07:38:04 UTC_  
