Get Discount In Percentage of product.Magento2



Try This in List Page same way you can do it in detail page.

<?php
        $specialprice = $_product->getSpecialPrice();
        $specialPriceFromDate = $_product->getSpecialFromDate();
        $specialPriceToDate = $_product->getSpecialToDate();    
        $today = time();
        $price = $_product->getPrice();
        if($price){ $sale = round((($price-$specialprice)/$price)*100); }
                               
        if ($specialprice) {
        if ($today >= strtotime($specialPriceFromDate) && $today <=                                                      strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate)&& is_null($specialPriceToDate)) { ?>
         <div class="discountPrice">
        <span class="sale-product-icon">
        <?php if($sale) { echo $sale.'%'; } else {echo __('Sale');} ?>
        </span>
        </div>
        <?php }
        }
?>

Comments

Popular posts from this blog

Through Postman Api send data through JSON and create simple product in Magento2

Getting product Image in PHTML in Magento2

programmaticlly Create Order in Magento2 with custom options in order