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
Hello @raju_a
<?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>
<?php namespace Vendor\Module\Block\Product; class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList { }
Make sure another module has not used the same preference in di.xml
Hope it helps.
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.
Is there any way to override widget grid.phtml without using of di.xml or preferences