2

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

2
  • have you added special price for products ? Mar 24, 2018 at 17:52
  • if you meant price rules - yes i did, it showed correctly (old and new price), but i'd like to show % which user will save.
    – bofh
    Mar 24, 2018 at 18:03

4 Answers 4

8

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.

19
  • Naveed, thanks for reply, but not helped. as i'm using standard luma theme, i have added it to vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml, also to vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml no luck(( cache cleaned and reindexed of course. maybe redeploy content?
    – bofh
    Mar 24, 2018 at 18:38
  • yes re deploy your static content, but it should work, unless you don't have created any custom theme having above files. as before posting solution i had implemented on luma theme myself and it worked Mar 25, 2018 at 6:23
  • unfortunately not helped... i have no custom theme, clean magento installation. could you pls confirm exact path is vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml? which magento version do you have? mine is 2.2.1
    – bofh
    Mar 25, 2018 at 9:42
  • could you update the question with screen shot of your theme directory and where you are making changes. Mar 25, 2018 at 10:27
  • have you added any special price ? also could you show me your theme files of Magneto_Catalog folder or you have not setup any theme yet ? Mar 25, 2018 at 13:09
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

1

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);
}
1
  • It will only work for simple product. Sep 24, 2019 at 11:00
1

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) . '%';
        }
    }

Your Answer

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

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