Categories: Magento 2 Tutorials

How to Override Model in Magento 2

A Model is an important part of MVC (Model-View-Controller) pattern as it represents the data of the application. In the same way, Models have a very useful role in Magento as well. Since editing Model from the core files is not a good practice, you should instead rewrite Model in Magento 2! Hence, in this tutorial, I am going to teach you how to override models in Magento 2. What you need is a custom module to do that, so let’s begin.

Steps to Follow

  • Create Directories
  • Configuration of Module
  • Registration of Module
  • Override di.xml
  • Override Product.php
  • Launch SSH and Run Commands

Create Directories

In the root directory of your store, create the following directories in the same manner shown below:

Configuration of Module

Create module.xml in app/code/Magenticians/Moduleproduct/etc and add the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Moduleproduct" setup_version="1.0.1">
</module>
</config>

Registration of Module

Create registration.php in app/code/Magenticians/Moduleproduct and add the following code in it:
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magenticians_Moduleproduct',
__DIR__
);

Override di.xml

Create di.xml  in app/code/Magenticians/Moduleproduct/etc in order to specify which model to override by using <preference for=””> and <preference type=””>. Add the following code in it: The purpose of creating this file is to override di.xml file
<?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="Magenticians\Moduleproduct\Model\Catalog\Product" />
</config>
<preference for=””> Which Model to override. <preference type=””> Where to override.

Override Product.php

Now create Product.php in app/code/Magenticians/Moduleproduct/Model/Catalog and add the following code in i
<?php

namespace Magenticians\Moduleproduct\Model\Catalog;

class Product extends \Magento\Catalog\Model\Product

{
   public function getName()
   {
       return $this->_getData(self::NAME) . ' + Demo Text';
   }

public function getSku()
   {
       return "123-Demo";
   }
}
In the code above, I have overriden the product name and product SKU.

Launch SSH and Run Commands

Run the following commands in the Magento 2 root directory:
php bin/magento module:enable Magenticians_Moduleproduct
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
Now it’s time to check results! Go to the product page and you should see this:

Looking for the most reliable hosting platform?

Then, do check out Managed Magento hosting and avail Free Trial for 3 Days.

Conclusion

After the implementation of all the steps above, you should be able to override Model in Magento 2. You can override more Models with the same approach depending on your requirements. If you still have any confusion related to this tutorial, just leave a comment below!
Syed Muneeb Ul Hasan

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.

View Comments

Share
Published by
Syed Muneeb Ul Hasan

Recent Posts

Building Employee Trust and Dedication – A Complete Guide

In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More

3 months ago

12 Winning Strategies for Small Businesses Marketing

In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More

3 months ago

Top 10 App Development Companies in Dubai 2024

With one of the highest internet penetration rates, the UAE has set out to revolutionize… Read More

3 months ago

Transforming Industries: How Amazons GPT44X is Revolutionizing AI Technology

Artificial Intelligence (AI) has been continually evolving, leading to remarkable advancements in various industries. Among… Read More

8 months ago

Top Magento 2 Extensions for 2023

Extensions, extensions and lots of extensions. We all love extensions, don’t we? After all, extensions… Read More

11 months ago

Unleashing the Power of Software Testing: Cooperating with a Testing Firm

Software quality is crucial to a firm's success across industries in the quickly changing digital… Read More

11 months ago