Featured Articles
By default, Magento does not provide to show the discount percentage of the product. Discounts have been the most effective tool to beat the competition, attract potential customers, and generate sales. For Magento 2 store owners, it is going to be easy with the solution given below to display percentage discount on the product details page.
First we need to create "catalog_product_prices.xml" at below location using the given code.
app\code\Mageefy\Discountpercentage\view\base\layout\catalog_product_prices.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">Mageefy_Discountpercentage::product/price/final_price.phtml</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</layout>
And after that create one more file to display the price and discount percentage on the product details page.
app\code\Mageefy\Discountpercentage\view\base\templates\product\price\final_price.phtml
<?php
$priceModel = $block->getPriceType('regular_price');
$finalPriceModel = $block->getPriceType('final_price');
$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;
?>
<?php if ($block->hasSpecialPrice()): ?>
<span class="special-price">
<?php echo $block->renderAmount($finalPriceModel->getAmount(), [
'display_label' => __('Special Price'),
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
</span>
<span>
<?php
$item = $block->getSaleableItem();
$_savePercent = 100 - round(((float)$item->getFinalPrice() / (float)$item->getPrice()) * 100);
echo '<b style="color:#008000">'.$_savePercent . '% off </b>';
?>
</span>
<span class="old-price">
<?php echo $block->renderAmount($priceModel->getAmount(), [
'display_label' => __('Regular Price'),
'price_id' => $block->getPriceId('old-price-' . $idSuffix),
'price_type' => 'oldPrice',
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
<?php else: ?>
<?php echo $block->renderAmount($finalPriceModel->getAmount(), [
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
<?php endif; ?>
<?php if ($block->showMinimalPrice()): ?>
<?php if ($block->getUseLinkForAsLowAs()):?>
<a href="<?= $block->getSaleableItem()->getProductUrl() ?>" class="minimal-price-link">
<?= $block->renderAmountMinimal() ?>
</a>
<?php else:?>
<span class="minimal-price-link">
<?= $block->renderAmountMinimal() ?>
</span>
<?php endif?>
<?php endif; ?>
That’s it.
Any doubts in the above solution? Please feel free to mention them in the Comments section below. I’d be glad to help you out.
Comments
Reviews
Most Recent Articles
- Jun 4, 2026 Famous Astrologer in Kolkata for Accurate Online Astrology Consultation by Alzbeta Berka
- Jun 4, 2026 Genuine Astrologer in Kolkata for Trusted Guidance and Life Solutions by Alzbeta Berka
- Jun 4, 2026 Astrology Services in Kolkata for Accurate Guidance and Life Solutions by Alzbeta Berka
- Jun 4, 2026 Best Astrologer Services in Kolkata for Holistic Astrology and Vastu Solutions by Alzbeta Berka
- May 4, 2026 Best Astrologer Services in Kolkata for Vastu & Holistic Guidance by Alzbeta Berka
Most Viewed Articles
- 3602 hits Bike Learning & Riding While Using Motorcycle Boots Houston & Leather Accessories by Motorcycle Accessories
- 2098 hits Shop a variety of trendy Muslim headscarf online by Lisa Stewart
- 2069 hits What To Know About Musician's Union Fees by Michael Welsh Productions
- 2066 hits Which is a right place to buy the 3D business cards? by E-plasticcards
- 1570 hits Importance of Custom-Designed Business Signs by Harry Landri
Popular Articles
In today’s competitive world, one must be knowledgeable about the latest online business that works effectively through seo services....
80901 Views
Walmart is being sued by a customer alleging racial discrimination. The customer who has filed a lawsuit against the retailer claims that it...
46310 Views
Are you caught in between seo companies introduced by a friend, researched by you, or advertised by a particular site? If that is...
36985 Views
Facebook, the best and most used social app in the world, has all the social features you need. However, one feature is missing. You cannot chat...
23312 Views
If you have an idea for a new product, you can start by performing a patent search. This will help you decide whether your idea could become the...
14510 Views
Moving becomes easy when you have the right moving accessories. These moving accessories help secure and protect your item by ensuring that no harm...
12187 Views
Moving from one state, city, or even to a whole different county, is something that is either dictated by choice or circumstance. This is because,...
11087 Views
A lot of us look forward to the result of moving and not the process itself. It is pretty typical behavior, though. As modern people, many things...
11023 Views
Building a custom home is an exciting adventure. It’s your chance to bring your vision to life and create an area that sincerely displays...
10789 Views
Statistics
| Members | |
|---|---|
| Members: | 16691 |
| Publishing | |
|---|---|
| Articles: | 78,294 |
| Categories: | 202 |
| Online | |
|---|---|
| Active Users: | 954 |
| Members: | 11 |
| Guests: | 943 |
| Bots: | 23714 |
| Visits last 24h (live): | 1776 |
| Visits last 24h (bots): | 46203 |