cancel
Showing results for 
Search instead for 
Did you mean: 

override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in module

override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in module

Hi guys i need to override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in my module, I tried 

https://magento.stackexchange.com/questions/97396/magento2-how-to-override-widget-template?noredirec... to override ,but its not working,can anyone help

3 REPLIES 3

Re: override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in module

Hello @raju_a 

 

 
  1. Create di.xml file at app/code/Vendor/Module/etc 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="Vendor\Module\Block\Product\ProductsList" />
    </config>
  2. Create a block file ProductsList.php at app/code/Vendor/Module/Block/Product to extend the widget class
    <?php
    namespace Vendor\Module\Block\Product;
    class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList
    {
    }
  3. Copy the related template file and put into your custom module with related Path
    vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml
    to
    app/code/Vendor/Module/view/frontend/templates/product/widget/content/grid.phtml

Make sure another module has not used the same preference in di.xml

 

Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in module

The issue with using preferences is that it could conflict with other modules overriding the same class. I would suggest injecting your custom template as a config option in etc/widget.xml.

 

<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget_file.xsd">
    <widget id="cms_static_block">
        <parameters>
            <parameter name="template" xsi:type="select">
                <options>
                    <option name="option_id" value="Custom_Module::view/custom-template.phtml">
                        <label translate="true">Custom template</label>
                    </option>
                </options>
            </parameter>
        </parameters>
    </widget>
</widgets>

 

With this approach, your template will be selectable as an option every time you create a new widget, and you won't have any conflict with other modules.

 

 

Re: override Magento_Catalog_Wiget/templates/product/widget/content/grid.phtml in module

Is there any way to override widget grid.phtml without using of di.xml or preferences