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












0















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










share|improve this question

























  • 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


















0















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










share|improve this question

























  • 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
















0












0








0








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










share|improve this 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







magento2.2 magento-2.2.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 12:51









Evince Development

1,112319




1,112319










asked Mar 24 '18 at 17:45









bofhbofh

4210




4210













  • 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





















  • 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



















have you added special price for products ?

– Naveed Asim
Mar 24 '18 at 17:52





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







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












2 Answers
2






active

oldest

votes


















2














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

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.






share|improve this answer


























  • 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











  • done, pls see above. thanks!

    – bofh
    Mar 25 '18 at 11:22



















0














To display product discount Price use the below code.
$blockObj= $block->getLayout()->createBlock('MagentoCatalogBlockProductPrice');
$_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);
}






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f219718%2fhow-to-display-product-discount-percent-on-product-details-page-in-magento-2-2-1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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

    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.






    share|improve this answer


























    • 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











    • done, pls see above. thanks!

      – bofh
      Mar 25 '18 at 11:22
















    2














    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

    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.






    share|improve this answer


























    • 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











    • done, pls see above. thanks!

      – bofh
      Mar 25 '18 at 11:22














    2












    2








    2







    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

    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.






    share|improve this answer















    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

    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 25 '18 at 18:09

























    answered Mar 24 '18 at 18:21









    Naveed AsimNaveed Asim

    2,5362315




    2,5362315













    • 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











    • done, pls see above. thanks!

      – bofh
      Mar 25 '18 at 11:22



















    • 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











    • done, pls see above. thanks!

      – bofh
      Mar 25 '18 at 11:22

















    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





    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







    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





    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





    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













    done, pls see above. thanks!

    – bofh
    Mar 25 '18 at 11:22





    done, pls see above. thanks!

    – bofh
    Mar 25 '18 at 11:22













    0














    To display product discount Price use the below code.
    $blockObj= $block->getLayout()->createBlock('MagentoCatalogBlockProductPrice');
    $_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);
    }






    share|improve this answer




























      0














      To display product discount Price use the below code.
      $blockObj= $block->getLayout()->createBlock('MagentoCatalogBlockProductPrice');
      $_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);
      }






      share|improve this answer


























        0












        0








        0







        To display product discount Price use the below code.
        $blockObj= $block->getLayout()->createBlock('MagentoCatalogBlockProductPrice');
        $_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);
        }






        share|improve this answer













        To display product discount Price use the below code.
        $blockObj= $block->getLayout()->createBlock('MagentoCatalogBlockProductPrice');
        $_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);
        }







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 11:15









        AneesAnees

        839




        839






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f219718%2fhow-to-display-product-discount-percent-on-product-details-page-in-magento-2-2-1%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1300-talet

            1300-talet

            Display a custom attribute below product name in the front-end Magento 1.9.3.8