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

Get Swatch Text Product id of Configurable Product in Detail Page through Jquery Magento2

Get Customer Address with customer ID programmatically Magento2

programmaticlly Create Order in Magento2 with custom options in order