As we all know Magento 2 is the latest upgrade in Magento and also a ecommerce-platform built on Open-Source Technology for making Ecommerce Websites for Online Merchants. It also comes with a lot of Default modules, but at some point we might want to change the default functionality as per our need.
The functionality can be anything like a change in its view or any module changes. As we all know if we try to modify or override a default Module in any framework, we might end up losing all our code if we update our project’s framework in future and that is why this method of modifying through source code is not recommended.
In Case of Magento we could modify or override our core modules without changing the source code directly. We Just need to create overriding codes in seperately in the app/code folder of our project as shown below :
So this will be the folder structure of our Product Overriding.
In this blog we will try to override the product name and SKU of a product in product details page, as shown below :
To Implement this we need to create a few files which will be shown further :
At first we will start with the registration of our Override Module.
So we will create a registration.php file to register our module :
The registration.php will be created here : app/code/Override/Product/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Override_Product',
__DIR__
);
And this will the code to register our module in magento 2.2.7
Now we need to create a file to configure our module named as module.xml
This file will be created under: app/code/Override/Product/etc/module.xml
<?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="Override_Product" setup_version="1.0.1">
</module>
</config>
Now Lets override our di.xml file, so that it uses our overriden code :
This file will be located in : app/code/Override/Product/etc/di.xml
<?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="Override\Product\Model\Catalog\Product" />
</config>
The for="Magento\Catalog\Model\Product" is used to show which file it has to override and type="Override\Product\Model\Catalog\Product" represents which file it has to use to override the code.
Now let’s create the main overriding file which will have the code for overriding the code. Also, this file should be stored in the exact folder structure of the default module after the Model folder in the custom one.
This file will be located in : app/code/Override/Product/Model/Catalog/Product.php
<?php
namespace Override\Product\Model\Catalog;
class Product extends \Magento\Catalog\Model\Product
{
public function getName(){
return $this->_getData(self::NAME)."+ Product Overrided";
}
public function getSku(){
return $this->getTypeInstance()->getSku($this)."+SKU Overrided";
}
}
As we see in the above methods the getName method is overrided with a text “ + Product Overrided ” at the end of return value, same goes for the method that gets the Sku.
One more thing that this code does is that it changes every product’s names on the product listing page as we see below :
This is how we can override a default module of magento 2.2.7 without even editing the source code of the default module. There are a lot of modules that can be overriden.
References
https://magenticians.com/override-model-magento-2/