How to display product discount percent on product details page in Magento 2.2.1

Am I wondering how to add percentage discount value on the product details page?
I have found a similar topic, but apparently, it does not work with 2.2.1

How to display product discount percent on product details page in Magento 2.1.8?enter image description here

upd:enter image description here

enter image description here

enter image description here

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

To show discount % on product detail page add below code where you want. This will show discount % either applied with Catalog Price rule or added Special price.

For example to show % near stock status add code in below template :
Theme/Magento_Catalog/templates/product/view/type/default.phtml.

<?php
$simplePrice = 0;
$_savingPercent = 0;
if($_product->getTypeId() == "simple") {
   $simplePrice = $_product->getPrice();
} else {
   $_children = $_product->getTypeInstance()->getUsedProducts($_product);
    foreach ($_children as $child){
    $simplePrice = $child->getPrice();
    break;
   }
}

$_finalPrice =$_product->getFinalPrice();
$_price = $simplePrice;
if($_finalPrice < $_price) {
   $_savingPercent = 100 - round(($_finalPrice / $_price)*100);
   echo 'Your save '.$_savingPercent . '%';

}
?>

NOTE : assuming all simple products of configurable have same price , however condition will pick only first simple product price of configurable product and calculate discount. But this logic / code block can be altered depending on the requirements.

Solution 2

To display the discount percentage in product view page.
Either you can override one of the below phtml.

vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml,
vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml

  $blockObj = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\Price');
 $_product = $blockObj->getProduct();
    $specialprice = $_product->getSpecialPrice();
       $price = $_product->getPrice();
    if ($price) {
       echo $sale = round(abs((($price-$specialprice)/$price)*100));
    }

Hope this will help !! Happy Coding

Solution 3

To display product discount Price use the below code.

$blockObj = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\Price');

$_product = $blockObj->getProduct();
$specialprice = $_product->getSpecialPrice();
$specialPriceFromDate = $_product->getSpecialFromDate();
$specialPriceToDate = $_product->getSpecialToDate();
$today = time();
$price = $_product->getPrice();

if ($price) {
   echo $sale = round((($price-$specialprice)/$price)*100);
}

Solution 4

Here’s a better way to get the discount amount

    public function getProductDiscount(ProductInterface $product)
    {
        $regularPrice = $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)->getAmount()->getValue();
        $finalPrice = $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)->getAmount()->getValue();

        if ($finalPrice < $regularPrice) {
            return round(100 - $finalPrice / $regularPrice * 100) . '%';
        }
    }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply