How to display product discount percent on product details page in Magento2?

by mageefy on Feb 9, 2023 Celebrities 310 Views

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.

visit- https://www.mageefy.com/blog/post/how-to-display-product-discount-percent-on-product-details-page-in-magento2

Article source: https://article-realm.com/article/Arts-Entertainment/Celebrities/36891-How-to-display-product-discount-percent-on-product-details-page-in-Magento2.html

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Most Viewed Articles

Statistics

Members
Members: 16519
Publishing
Articles: 77,894
Categories: 202
Online
Active Users: 351
Members: 2
Guests: 349
Bots: 8918
Visits last 24h (live): 1777
Visits last 24h (bots): 43637

Latest Comments

Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job 짱구토토...
비슷한 주제에 대한 흥미로운 정보를 읽을 수있는 페이지로 여러분을 초대합니다. 푸우카지노    
I was impressed to learn that Atithi Home offers Homestay rooms with cooking facilities in Guwahati , making it ideal for long-term guests and families.
Price action is a powerful strategy, and Share Market Skills offers a valuable Price action trading course in Ghaziabad for serious market participants.
Businesses need websites that are both attractive and effective. Digital Solution 360 provides outstanding Website Development Services in Al Ain to meet those needs.
I really enjoyed reading this. Preesha Global is a trusted name for bookkeeping services Florida , helping businesses stay organized and financially healthy.
Thanks for the detailed explanation. Packers and Movers Jalandhar stands out among the best packers and movers in Jalandha r due to their professional approach.
The growing demand for plant-based foods shows that more consumers are looking for healthier and specialized dining options. This trend is also reflected in restaurant menus, where people often...
Strands NYT brings a refreshing twist to traditional word search games by combining themed puzzles with the clever spangram mechanic. What I enjoy most is that the game rewards both vocabulary...
on Jun 18, 2026 about Strands NYT game Review
This is a fascinating breakdown of casing cementing! The attention to detail needed for "cement slurry design" and "cement placement" sounds intense. It reminds me a bit of trying to precisely...
on Jun 18, 2026 about Casing cementing process

Translate To: