7

I want to override final_price.phtml, I have tried following in my module but it is not working. I have created app/code/MyVendor/MyModule/view/frontend/layout/catalog_product_prices.xml

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="final_price" xsi:type="array">
                        <!-- item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item> -->
                        <item name="render_template" xsi:type="string">MyVendor_MyModule::product/price/final_price.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</layout> 

Also I have created template file as well. at app/code/MyVendor/MyModule/view/frontend/templates/product/price/final_price.phtml And in final_price.phtml I wrote my custom code.

But it is not working. Can anyone tell me what is missing in this code?

  • Have u flush magento cache after that? – Amit Bera Mar 24 '17 at 11:36
  • Yes I checked Cache is disabled. – Dhaval Solanki Mar 24 '17 at 11:56
  • Did you ever found how to do that via layouts ? Because the plug-in solution is a bit awfull... – Pol Ravalitera Aug 30 '17 at 12:53
  • @pol No I don't know if its possible with layout or not. – Dhaval Solanki Sep 1 '17 at 11:16
  • check error log and comment it here – Sameer Bhayani Jan 9 '18 at 7:04
11

You can use alternative way for overriding template. Use below code. It will work.

app/code/MyVendor/MyModule/etc/di.xml

<type name="\Magento\Catalog\Pricing\Render\FinalPriceBox">
        <plugin name="MyVendor_MyModule_change_template" type="MyVendor\MyModule\Plugin\FinalPricePlugin" />
</type>

MyVendor\MyModule\Plugin\FinalPricePlugin.php

<?php
namespace MyVendor\MyModule\Plugin;

class FinalPricePlugin
{
    public function beforeSetTemplate(\Magento\Catalog\Pricing\Render\FinalPriceBox $subject, $template)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $enable=$objectManager->create('MyVendor\MyModule\Helper\Data')->chkIsModuleEnable();
        if ($enable) {
            if ($template == 'Magento_Catalog::product/price/final_price.phtml') {
                return ['MyVendor_MyModule::product/price/final_price.phtml'];
            } 
            else
            {
                return [$template];
            }
        } else {
            return[$template];
        }
    }
}

Hope it will work for you.

  • Happy to hear that it helps you. – Kishan Patadia Mar 24 '17 at 13:30
  • very helpful @kishan Patadia – Ramkishan Suthar Oct 25 '17 at 12:35
  • A-W-E-S-O-M-E-! – Pini Jun 4 '18 at 11:09
  • 2
    using plugin is not good solution, because if you have multiple theme then it always take one template mentioned in plugin , so from layout xml set your custom template which is mentioned in my below post – Pradeep Kumar Jul 27 '18 at 5:05
1

No need to write plugin for this, we have to do it in xml level

create a layout xml

app\code\MYNAME_SPACE\MY_MODULE\view\base\layout\catalog_product_prices.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="final_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">MYNAME_SPACE_MY_MODULE::product/price/final_price.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</layout>

Above xml will effect only for simple product if you need to modify the bundle product then

<argument name="default" xsi:type="array">

should be

<argument name="bundle" xsi:type="array">

or in case of a configurable product

<argument name="configurable" xsi:type="array">

create a phtml that is

\app\code\MYNAME_SPACE\MY_MODULE\view\base\templates\product\price\final_price.phtml

Copy final_price from core module and modify it as you needed

to modify other price type you can refer

vendor\magento\module-bundle\view\base\layout\catalog_product_prices.xml

  • This did not work for me. Using Magento 2.2.0. – jdhildeb May 21 at 21:26
0

First check your final_price.phtml for any errors.

Now flush and clean the cache using cli commands

bin/magento cache:clean

bin/magento cache:flush

Now refresh.

  • No final_price.phtml don't have any error I have checked. and cache is disabled then also I tried above command but no luck. – Dhaval Solanki Mar 24 '17 at 11:56
0

I had the same problem that I could not overwrite final_price.phml. I solved the problem by adding sequence to the module.xml with Magento_Catalog (and then setup:upgrade):

    <sequence>
        <module name="Magento_Catalog" />
    </sequence> 
0

I was able to override final_price.phtml by re-creating the directory under my theme.

Original file lives

vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml

New file lives here

/app/design/frontend/NAMESPACE/MYTHEME/Magento_Catalog/templates/product/price/final_price.phtml
0
  1. Create app/code/Vendor/Module/view/base/layout/catalog_product_prices.xml:
    <?xml version="1.0"?>
    <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
        <referenceBlock name="render.product.prices">
            <arguments>
                <argument name="configurable" xsi:type="array">
                    <item name="prices" xsi:type="array">
                        <item name="final_price" xsi:type="array">
                            <item name="render_template" xsi:type="string">Cloudinfo_ChangesTheme::product/price/final_price.phtml</item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </layout>
  1. Add in module.xml:
       <sequence>
           <module name="Magento_Catalog" />
       </sequence>
  1. Create app/code/Vendor/Moduleview/base/templates/product/price/final_price.phtml

  2. bin/magento setup:upgrade && bin/magento setup:di:compile && bin/magento setup:static-content:deploy && bin/magento c:c && bin/magento c:f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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