3

I need to override the widget template file /vendor/magento/module-catalog/view/frontend/templates/product/widget/new/content/new_grid.phtml but in xml file the phtml file was not mentioned at any where except on /etc folder.

<parameter name="template" xsi:type="select" required="true" visible="true">
    <label translate="true">Template</label>
    <options>
        <option name="default" value="product/widget/new/content/new_grid.phtml" selected="true">
            <label translate="true">New Products Grid Template</label>
        </option>
    </options>
</parameter>

How can I override the vendor-widget-phtml file new_grid.phtml to my custom module.

1 Answer 1

6

this way i get changes of phtml by my custom module

app/code/VendorName/ModuleName/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">

    <preference for="Magento\Catalog\Block\Product\Widget\NewWidget" type="MyModule\WidgetOverride\Block\Product\Widget\NewWidget" />

</config>

app/code/VendorName/ModuleName/Block/Product/Widget/NewWidget.php

<?php
namespace MyModule\WidgetOverride\Block\Product\Widget;

class NewWidget extends \Magento\Catalog\Block\Product\Widget\NewWidget
{
  protected function _construct()
    {
      parent::_construct();
      $this->setTemplate('VendorName_ModuleName::product/widget/content/new_grid.phtml');
    }
}

now add your phtml file this path app/code/VendorName/ModuleName/view/frontend/templates/product/widget/new/content/new_grid.phtml

frontend shows my changes on product page by override phtml file

enter image description here hope this help you :) Happy coding.

6
  • I have tried this but still the changes I'm doing in the phtml is not showing on frontend.
    – MagentoDev
    Commented Oct 22, 2020 at 6:55
  • Thanks. I will check and update you
    – MagentoDev
    Commented Oct 22, 2020 at 11:33
  • I did try your code but still the update is not showing in frontend.
    – MagentoDev
    Commented Oct 23, 2020 at 13:22
  • yes I have ran all the commands
    – MagentoDev
    Commented Oct 23, 2020 at 16:11
  • I need to show the product price only when we set the custom attribute status as approved from dropdown. I have hide the price by following this, magento.stackexchange.com/questions/165752/… but need the ouput as above mentioned. Please guide
    – MagentoDev
    Commented Oct 26, 2020 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.