Overriding Models in Magento 2

Overriding Models in Magento 2

Whenever we encounter this scenario with our clients who own Magento online stores, we gauge the need to rewrite the core features of Magento in order to cater to the client’s requirements. There might be plugins to help you achieve what you’re trying to do but more often than not, the requirements are so unique that we get down to overriding core models of the Magento. And needless to say, we love getting our hands dirty with code ;)

Statutory warning: Even a newbie to Magento stores knows very well, that it is not advisable to tamper with the core model files. What we mean is: NEVER “UPDATE/ALTER/MODIFY” the core models in your Magento. We suggest “OVERRIDING” instead – that way you can revert in a jiffy. Models are a criticalcomponent of modules since they’re the source of data manipulations and business logic.

Without further ado, here’s how you can override core models in Magento2:

Our use case here is simple – we want to append a text “VelanApps” to all products on the Magento Store, as illustrated in the image below:

Magento 2 Overriding Models Display in Admin Panel

To do this, let’s start by creating a new module TESTING:

Module registration file:

Path: app/code/Velanapps/Testing/registration.php

Module Declaration file:

Path: app/code/Velanapps/Testing/etc/module.xml
<?xml version="1.0"?>
<module name="Velanapps_Testing" setup_version="1.0.0"/>

In Magento 2, we use the di.xml file to enforce the override.

Model override using di.xml file:

Path: app/code/Velanapps/Testing/etc/di.xml
<?xml version="1.0"?>
              <preference for="Magento\Catalog\Model\Product" type="Velanapps\Testing\Model\Catalog\Product" />

For our scenario, we are overriding the Product Model present in the Catalog module with our newly created Testing Module. If you look closely at the di.xml code:

for="Magento\Catalog\Model\Product"  -> Core Model
type="Velanapps\Testing\Model\Catalog\Product" -> New Module


New lets proceed with creating our Product Model file, under our newly created Testing Module. To append a text “- Velanapps” every time the Product name gets called, we need to rewrite the getName() method in this file The following code will append a string to the product name every time the product name is called:

                namespace Velanapps\Testing\Model\Catalog;
                class Product extends \Magento\Catalog\Model\Product
                                public function getName()
                                    return $this->_getData(self::NAME) . ' - Velanapps';

With that done, whenever we call any product’s name on your Magento store, this string will be appended to the product name. It’s that simple!

Now that we have everything setup in our module, let’s run some Magento 2 commands and watch the magic on your Magento store!

php bin/magento cache:disable
php bin/magento deploy:mode:set developer
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

You can skip the second command if you wish, but we highly recommend turning on Developer mode when your Magento store is under development phase.

As the final step, load the admin panel or the frontend,and Voila, our text will automatically be appended to the product names everywhere:

Magento 2 Overriding Models Display in Admin Panel

Magento 2 Overriding Models display in Frontend

Try it out! Did you find this tutorial useful? Do let us know in the comments! If you’re stuck somewhere then hit us up because we’re very active on this blog!

By the way, for all your Magento online store needs, head over to our Magento Extensions Store and we’re sure you will find what you’re looking for!

Write Your Comment

Only registered users can write comments. Please, log in or register