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.

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.

| improve this answer | |
3

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); ?>
| improve this answer | |
  • 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 '17 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 '18 at 9:29
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.

| improve this answer | |
  • 1
    How to ovveride file 'FinalPriceBox.php'? – Ketan Borada Sep 11 '17 at 11:48
  • this work only simple product type only – HaFiz Umer Nov 18 '19 at 13:51
2

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();
| improve this answer | |

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.