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

  • have you added special price for products ? – Naveed Asim Mar 24 '18 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 '18 at 18:03
3

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.

  • 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 '18 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 – Naveed Asim Mar 25 '18 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 '18 at 9:42
  • could you update the question with screen shot of your theme directory and where you are making changes. – Naveed Asim Mar 25 '18 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 ? – Naveed Asim Mar 25 '18 at 13:09
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);
}
  • It will only work for simple product. – Rahul Sep 24 '19 at 11:00
1

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.phtm

  $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

0

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, privacy policy and cookie policy

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