In today’s blog post, you will learn about how to override the helper, model, block, and controller in Magento2. For example, we will be overriding product helper, list product block, product model, and product view controller.
Overriding Magento 2 Helper
Step – 1: Create a di.xml file in a folder Webiators/CustomChanges/etc
1 2 |
<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="Webiators\CustomChanges\Helper\Rewrite\Product" /> </config> |
Step – 2: The next step to overriding Magento2 Helper is to create a Product.php Block file in the folder Webiators/CustomChanges/Helper/Rewrite/Product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<php /** * @category Webiators * @package Webiators_CustomChanges * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ namespace Webiators\CustomChanges\Helper\Rewrite; class Product extends \Magento\Catalog\Helper\Product { public function __construct() { echo "Helper Rewrite Working"; die(); } } You can rewrite other Magento 2 helper using the same method. |
Overriding Magento 2 Blocks
Step – 1: Create a di.xml file in a folder Webiators/CustomChanges/etc
1 2 3 4 |
<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="Webiators\CustomChanges\Block\Rewrite\Product\ListProduct" /> </config> |
Step – 2 The next step to overriding Magento2 block is to create a ListProduct.php Block file in the folder Webiators/CustomChanges/Block/Rewrite/Product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<php /** * @category Webiators * @package Webiators_CustomChanges * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ namespace Webiators\CustomChanges\Block\Rewrite\Product; class ListProduct extends \Magento\Catalog\Block\Product\ListProduct { public function __construct() { echo "Block Rewrite Working"; die(); } } You can rewrite other Magento 2 blocks using the same method. |
Overriding Magento 2 Model
Step – 1: Create a di.xml file in Folder Webiators/CustomChanges/etc
1 2 3 4 |
<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="Webiators\CustomChanges\Model\Rewrite\Catalog\Product" /> </config> |
Step – 2: Create Product.php Model file in Folder Webiators/CustomChanges/Model/Rewrite/Catalog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<php /** * @category Webiators * @package Webiators_CustomChanges * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ namespace Webiators\CustomChanges\Model\Rewrite\Product; class ListProduct extends \Magento\Catalog\Model\Product { public function __construct() { echo "Model Rewrite Working"; die(); } } You can rewrite other Magento 2 model using the same method. |
Overriding Magento 2 Controller
Step – 1: Just like for Models and Blocks create di.xml file in folder Webiators/CustomChanges/etc
1 2 3 4 5 6 |
Step - 1: Just like for Models and Blocks create di.xml file in folder Webiators/CustomChanges/etc <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="Webiators\CustomChanges\Controller\Rewrite \Product\View" /> </config> |
Step – 2: In the second step of overriding controller in Magento 2, create a View.php Controller file in Folder Webiators/CustomChanges/Controller/Rewrite/Product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<php /** * @category Webiators * @package Webiators_CustomChanges * @author Webiators Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://store.webiators.com ). */ namespace Webiators\CustomChanges\Controller\Rewrite\Product; class ListProduct extends \Magento\Catalog\Controller\Product\View { public function __construct() { echo "Controller Rewrite Working"; die(); } } You can rewrite other Magento 2 controller using the same method. |