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

by mageefy on Feb 9, 2023 Celebrities 336 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: 16927
Publishing
Articles: 79,052
Categories: 202
Online
Active Users: 4388
Members: 15
Guests: 4373
Bots: 20217
Visits last 24h (live): 9953
Visits last 24h (bots): 51025

Latest Comments

This is a really helpful guide for anyone facing a move! Planning ahead truly makes a huge difference. I appreciate the clear breakdown of common pitfalls, especially the advice about decluttering...
The Escorts Aerocity offers are simply unmatched in beauty and skill. These passionate companions know exactly how to please, ensuring every moment together is filled with intense pleasure and...
I came across SSS TikTok while looking for a simple way to download TikTok videos, and honestly, the process is really straightforward. You don’t have to deal with complicated steps or...
on Aug 25, 2026 about The Latest Online Business
Hi there, I found your blog via Google while searching for such kinda informative post and your post looks very interesting for me.  RC Mower
It's always exciting to read articles from other writers and practice something from their web sites   벳플레이주소    
I am really impressed with your writing skills well with the layout for your weblog. Is that this a paid subject matter or did you modify it your self? Anyway stay up the nice high quality...
주목할만한 기사, 특히 유용합니다! 나는 이것에서 조용히 시작했고 더 잘 알게되고 있습니다! 기쁨, 계속해서 더 인상적  띵벳 도메인 주소  
귀하가 게시 한 정보는 매우 유용합니다. 추천 한 사이트가 좋았습니다. 공유 해주셔서 감사합니다   betplay    
Finding genuine companionship that matches your high standards can sometimes be a challenging task in a massive metropolis. However, utilizing a premier VIP Delhi Call Girls Service...
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon....

Translate To: