Configuration of Module
Create module.xml file at app/code/Magemonkeys/RewriteProduct/etc and add the following code inside this file:
<?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="Magemonkeys_RewriteProduct" setup_version="1.0.1"> </module> </config>
Registration of Module
Create registration.php file at app/code/Magemonkeys/RewriteProduct and add the following code inside this file:
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'Magemonkeys_RewriteProduct', __DIR__ );
Override di.xml
Create di.xml file at app/code/Magemonkeys/RewriteProduct/etc in order to specify which model to override by using <preference for=””> and <preference type=””>.
Add the following code inside dl.xml file: 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="MagentoCatalogModelProduct" type="MagemonkeysRewriteProductModelCatalogProduct" /> </config>
Override Product.php
Now, create Product.php file at app/code/Magemonkeys/RewriteProduct/Model/Catalog and add the following code in this file.
<?php
namespace MagemonkeysRewriteProductModelCatalog;
class Product extends MagentoCatalogModelProduct
{
public function getName()
{
return $this->_getData(self::NAME) . ' + Override Model - Name';
}
public function getSku()
{
return "123-Override Model - Sku";
}
}
Launch SSH and Run Commands
Run the following commands in the Magento 2 root directory:
php bin/magento module:enable Magemonkeys_RewriteProduct 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 the result!
Go to the product page and you should see as below:


