Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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);
}

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) . '%';
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top