Magento 2 – Change Price Template Only for Product View Page

magento2pricetemplate

as far as I understand this the price is rendered in:

/app/code/Magento/Catalog/view/base/templates/product/price/amount/default.phtml

But as I change it, it gets, of course, changed for every part of the site (like category view).

How can I change how my price is displayed in my product view? If I for example want to add labels for price incl. tax and excl. tax, tax amount, etc..?

Best Answer

In catalog_product_view.xml there is a block (product.price.render.default) which is responsible for price rendering.

That block you can find in below paths:

  1. /vendor/magento/module-catalog/view/adminhtml/layout/CATALOG_PRODUCT_COMPOSITE_CONFIGURE.xml
  2. /vendor/magento/module-catalog/view/base/layout/default.xml

which is this:

<block class="Magento\Framework\Pricing\Render" name="product.price.render.default">
        <arguments>
            <argument name="price_render_handle" xsi:type="string">catalog_product_prices</argument>
            <argument name="use_link_for_as_low_as" xsi:type="boolean">true</argument>
            <!-- set "override" configuration settings here -->
        </arguments>
    </block>

You can copy this block to catalog_product_view.xml inside body tag and change this

<argument name="price_render_handle" xsi:type="string">catalog_product_prices</argument>

to this

<argument name="price_render_handle" xsi:type="string">custom_catalog_product_prices</argument>.

Then create a new xml layout file with name custom_catalog_product_prices.xml and copy content from file /vendor/magento/module-catalog/view/base/layout/catalog_product_prices.xml.

After copy content you can see there is so many template set in this xml layout now you can modify templates according to you in custom_catalog_product_prices.xml file.

Related Topic