There is always a certain level of customization required for every Magento project. This can involve adding entirely new elements or overriding the existing ones. Customizing your Magento store according to your preferences gives you a personalized feel and ease of use while handling tasks. It also improves the customer user experience in several levels that can benefit your business with increased conversions and revenues. However, it is never recommended to modify the core files of Magento community or enterprise version. Instead of completely modifying core files, Magento recommends to override/overwrite the core files if needed.
Today, we will guide you about overriding helper, model, block and controller in Magento2. Here in example we will be overriding product helper, list product block, product model and product view controller. You can use the similar approach to override other block, model and controllers in Magento.
You can also contact FMEExtensions for Magento custom development or hire a dedicated Magento developer to handle your store management and customization tasks.
Let's start with overriding core Product helper. Suppose you need to make some additions in Magento 2 Product helper.
Step # 1 – Create a di.xml file in a folder FME/Test/etcStep # 2 - The next step to overriding Magento2 Helper is to create a Product.php Block file in the folder FME/Test/Helper/Rewrite/Product<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Helper\Product" type="FME\Test\Helper\Rewrite\Product" /> </config>
You can rewrite other Magento 2 helper using the same method.<php /** * Catalog Product Rewrite Helper * * @category FME * @package FME_Test * @author Fme Extensions Development Company * */ namespace FME\Test\Helper\Rewrite; class Product extends \Magento\Catalog\Helper\Product { public function __construct() { echo "Helper Rewrite Working"; die(); } }
Step # 2 - The next step to overriding Magento2 block is to create a ListProduct.php Block file in the folder FME/Test/Block/Rewrite/Product<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Product\ListProduct" type="FME\Test\Block\Rewrite\Product\ListProduct" /> </config>
You can rewrite other Magento 2 blocks using the same method.<php /** * Rewrite Product ListProduct Block * @category FME * @package FME_Test * @author Fme Extensions Development Company */ namespace FME\Test\Block\Rewrite\Product; class ListProduct extends \Magento\Catalog\Block\Product\ListProduct { public function __construct() { echo "Block Rewrite Working"; die(); } }
Step # 2 Create Product.php Model file in Folder FME/Test/Model/Rewrite/Catalog<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="FME\Test\Model\Rewrite\Catalog\Product" /> </config>
You can rewrite other Magento 2 model using the same method.<php /** * Catalog Product Rewrite Model * @category FME * @package FME_Test * @author Fme Extensions Development Company */ namespace FME\Test\Model\Rewrite\Catalog; class Product extends \Magento\Catalog\Model\Product { public function __construct() { echo "Model Rewrite Working"; die(); } }
Step # 2: In the second step of overriding controller in Magento 2, create a View.php Controller file in Folder Fme/Test/Controller/Rewrite/Product<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Controller\Product\View" type="FME\Test\Controller\Rewrite \Product\View" /> </config>
<?php /** * Rewrite Product View Controller * @category FME * @package FME_Test * @author Fme Extensions Development Company */ namespace FME\Test\Controller\Rewrite\Product; class View extends \Magento\Catalog\Controller\Product\View { public function execute() { echo "Controller Rewrite Working"; die(); } }
You can rewrite other Magento 2 controller using the same method.
Recommended Articles: