4

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..?

7

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.

| improve this answer | |
  • This does not seem to work, although I am unsure how i should add the template. Just like template="Module_Vendor::product/price/amount/custom.phtml" ? – TrytoFly Jan 23 '18 at 18:03
  • i have updated my answer please check. – dev_67Commerce Jan 24 '18 at 6:39
  • 1
    there is one more catalog_product_prices.xml in Magento_Tax module, please explore and checke what changes you can append into your own. – dev_67Commerce Jan 29 '18 at 6:07
  • 1
    this seems a long way from the usual "add template in correct location and hope for the best", I'm wondering why .. anyway thank you ! – medmek Sep 21 '18 at 16:46
  • 1
    Agree with medmek's comment. Simply adding override files with the same names, into you own theme will do the trick. In my case, I wanted to change the order of the special and regular price blocks (without css, if possible) and change the 'Regular Price' label to 'Retail Price Was'. To do this, I copied the 'vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml' to my theme, made the text and layout order updates I needed and voila, it works. – Digital_Frankenstein Oct 1 '18 at 14:07
0

I've run into this problem several times. The client often wants a different price display based on where the customer is viewing a product. Category page vs product detail page would be one example.

My solution is to override vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml and place it in your own theme folder.

From there you can use block methods to retrieve the current layout handle using $block->getRequest()->getFullActionName() that will return a value based on the page you are viewing

  • catalog_product_view (Product Detail Page)
  • catalog_category_view (Category Listing)
  • Just echo the handle for any other pages with prices to find others

You can then use the layout handle in an if statement and change display of the price based on layout handle

$handle = $block->getRequest()->getFullActionName();
if($handle == 'catalog_product_view') {
    //Product price display
} else {
    //Default price display
}
| improve this answer | |

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.