Rewrite an Existing Magento 2 Model

This article will help you understand how to rewrite an existing Magento 2 model. In earlier versions of Magento, you had to specify the rewrite within the module’s config.xml file. In Magento 2, this is done in the module’s di.xml file.

This article will show an example of how to override the Magento\Catalog\Model\Product class and its getName() method.

If you don’t have a custom module structure set up, create one by starting with the module.xml file.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Siphor_Custom" setup_version="0.0.1" />
</config>

And lastly, register the custom module by adding a registration.php file.


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Siphor_Custom',
    __DIR__
);

Run the command to enable the module from within the Magento root directory.

$ /path/to/your/php bin/magento module:enable --clear-static-content Siphor_Custom

After doing this and refreshing your Magento store, you might get encounter the following error.

Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory. 
The following modules are outdated:
Siphor_Custom schema: current version - none, required version - 0.0.1
Siphor_Custom data: current version - none, required version - 0.0.1

To rectify this, run the following command.

$ /path/to/your/php bin/magento setup:upgrade

Now that the custom module has been registered, the module’s di.xml file can be added.

This is where the model override will be specified. The key information resides in the <preference> node.

The for attribute value specifies the class you intend to override, and the type attribute specifies your custom class name.


<?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="Siphor\Custom\Model\Catalog\Product" />
</config>

Finally, the custom model class can be added with the method override.


<?php
namespace Siphor\Custom\Model\Catalog;

use Magento\Catalog\Model\Product as MagentoCatalogModelProduct;

class Product extends MagentoCatalogModelProduct
{
    /**
     * Get product name
     *
     * @return string
     * @codeCoverageIgnoreStart
     */
    public function getName()
    {
        return $this->_getData(self::NAME) . ' and something extra!';
    }
}

Here the string and something extra! is the text that will get added when the product’s name is rendered on the product pages.

If you then head to any of your product pages, you’ll see the additional text appended to the product name.

Rewrite an Existing Magento 2 Model

Although this is a basic example of how to rewrite an existing Magento 2 model, the same steps apply to more complex models that need to be rewritten. When using this method, also check for any existing rewrites if there are any, to avoid rewrite conflicts.

It might be possible to rewrite a model using Plugins, which give you the ability to extend functionality without creating rewrite conflicts. View the post for more information.

Note: This article is based on Magento Community/Open Source version 2.1.