4

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.

3

4 Answers 4

5

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

<?php
    $specialprice = $_product->getSpecialPrice();
    $specialPriceFromDate = $_product->getSpecialFromDate();
    $specialPriceToDate = $_product->getSpecialToDate();    
    $today = time();
    $price = $_product->getPrice();
    if($price){
        $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');} ?>
                </span>
            </div>
        <?php }
    }
?>

The same way you can do it from product page.

4

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

Create below helper function in your module

<?php

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); ?>
2
  • 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
    Commented 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
    Commented May 18, 2018 at 9:29
3

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();
2

You have to overwrite

vendor/magento/module-catalog/Pricing/Render/FinalPriceBox.php

file and

vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.