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

by mageefy on Feb 9, 2023 Celebrities 343 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: 17030
Publishing
Articles: 79,412
Categories: 202
Online
Active Users: 4482
Members: 3
Guests: 4479
Bots: 23836
Visits last 24h (live): 72676
Visits last 24h (bots): 62648

Latest Comments

이 주제에 대한 흥미롭고 흥미로운 정보는 볼 가치가있는 프로필에서 찾을 수 있습니다. 짱구 도메인 공식 주소  
안녕하세요. GOOGLE을 사용하여 블로그를 찾았습니다. 이것은 아주 잘 쓰여진 기사입니다. 나는 그것을 북마크하고 당신의 유용한 정보를 더 읽기 위해 돌아올 것입니다. 게시물 주셔서 감사합니다. 꼭 돌아 올게요.   나루토벳    
Pemerintah Indonesia, persetan dengan situs web ini, mereka menipu orang. maxslot88 scam  
Para penipu yang memalukan memangsa orang-orang dengan klaim palsu dan kebohongan.  maxslot88 scam    
Pemerintah Indonesia, persetan dengan para penipu itu, tangkap mereka dan persetan dengan mereka karena telah menipu banyak orang. maxslot88 scam  
Halaman ini dikelola oleh para penipu brengsek, hanya berisi kebohongan, janji palsu, dan trik murahan. Hindari orang-orang bodoh ini.  maxslot88 scam  
귀하가 게시 한 정보는 매우 유용합니다. 추천 한 사이트가 좋았습니다. 공유 해주셔서 감사합니다    네임드벳    
High Class Call Girl Delhi was ex-porn star famous. Recognized her from videos instantly. Fucked like professional on camera. Positions were athletic and deep. She knew angles for pleasure....
나는 그들이 매우 도움이 될 것이라고 확신하기 때문에 사람들을 귀하의 사이트로 다시 보내기 위해 귀하의 사이트를 내 소셜 미디어 계정에 추가하고 공유했습니다.  네임드벳    
IPTV trials can be a useful way to test streaming quality, channel variety, and app compatibility before committing to a paid subscription. I’d definitely check the trial terms carefully,...
on Sep 14, 2026 about IPTV PAID AND TRIAL SUBSCRIPTIONS

Translate To: