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?

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

6 Answers 6

13

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.

8
  • Happy to hear that it helps you. Mar 24, 2017 at 13:30
  • very helpful @kishan Patadia Oct 25, 2017 at 12:35
  • A-W-E-S-O-M-E-!
    – Pini
    Jun 4, 2018 at 11:09
  • 3
    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 Jul 27, 2018 at 5:05
  • please post full content of file di.xml. Your code is only part of it and it's not working
    – zekia
    Mar 5, 2020 at 14:15
4

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

1
  • This did not work for me. Using Magento 2.2.0.
    – jdhildeb
    May 21, 2019 at 21:26
2

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
1
  • 1
    thats's not work in Magento 2.3
    – Charly
    Mar 23, 2020 at 8:05
1
  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

1
  • is this concept will work on M2.3.4 ? Apr 7, 2020 at 11:25
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.

1
  • 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. Mar 24, 2017 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> 

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.