I want to display discount in percentage format on product list and product view page as like below

30% Discount

How I can achieve this task in proper Magento-2 way.

Code should be define in any one place and use it in anywhere.

thanks in advance.


4 Answers 4


Try this in design/frontend/Vendor_Name/Theme_Name/Magento_Catalog/templates/product/list.phtml

    $specialprice = $_product->getSpecialPrice();
    $specialPriceFromDate = $_product->getSpecialFromDate();
    $specialPriceToDate = $_product->getSpecialToDate();    
    $today = time();
    $price = $_product->getPrice();
        $sale = round((($price-$specialprice)/$price)*100);
    if ($specialprice) {
        if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) { ?>
            <div class="label-product label-sale">
                <span class="sale-product-icon">
                    <?php if($sale) { echo $sale.'%'; } else {echo __('Sale');} ?>
        <?php }

The same way you can do it from product page.


I have completed it by bellow code for display in any product list.

Create below helper function in your module


namespace VendorName\ModuleName\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper

    public function DisplayDiscountLabel($_product)
        $originalPrice = $_product->getPrice();
        $finalPrice = $_product->getFinalPrice();

        $percentage = 0;
        if ($originalPrice > $finalPrice) {
            $percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);

        if ($percentage) {
            return $percentage."% DISCOUNT";


You can call it any product list like below code

<?php echo $this->helper('VendorName\ModuleName\Helper\Data')->DisplayDiscountLabel($_product); ?>
  • You did what I advised you with only a difference: you implemented DisplayDiscountLabel method in helper instead of in block.. that it should be the right place.
    – WaPoNe
    Jan 24, 2017 at 10:51
  • Thanks Suresh, Above code will not work when catalog price rule applied on the product. Do you have any solution for this?
    – Shivam
    May 18, 2018 at 9:29

If you set discounts via Catalog Price Rules, you'll get wrong final prices by methods like

$finalPrice = $_product->getFinalPrice();
// or
$specialprice = $_product->getSpecialPrice();

It works only for special prices set in products. But for catalog price rules $finalPrice will be equal $originalPrice

This works:

$finalPrice = $_product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();

You have to overwrite


file and


template into your module.

In the first file you should add and implements your method to calculate product discount percentage; in the second one (template) you should call the method.

  • 1
    How to ovveride file 'FinalPriceBox.php'? Sep 11, 2017 at 11:48
  • this work only simple product type only
    – HaFiz Umer
    Nov 18, 2019 at 13:51

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.