Magento2: Override Catalog Widget Block

When we override the widget block class, then due to some reason, we need to put related template file into the override block with the same directory path as original.

That’s why to override the catalogwidget, we need to follow given below steps:

1- Create di.xml file into your custom module.

<?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\CatalogWidget\Block\Product\ProductsList" type="BrijeshTech\CatalogWidget\Block\Product\ProductsList" />
</config>

 

2- Create a block file to extend the widget class:

<?php

namespace &nbsp; BrijeshTech\CatalogWidget\Block\Product;

class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList
{
    public function customMethod()
    {
       //do your action here
    }
}

?>

 

3- Copy the related template file and put into your custom module with related directory Path:

For this example, copy the file by going into given below path.

vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml

 

And put into your custom module’s view ditectory into given below path:

app/code/BrijeshTech/CatalogWidget/view/frontend/templates/product/widget/content/grid.phtml

Hope, by this way your problem got fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *