Magento 2 Configurable Product – How to Get Price

configurable-productmagento2

I m working on ../Magento_Catalog/templates/product/list.phtml. I already write onsale label for products like this;

<span class="onsale">
    <span>
     <?php
     $label_price=$_product->getPrice();
     $final_price=$_product->getSpecialPrice();
     $save_percent = 100 - round(($final_price / $label_price)*100);
     echo $save_percent.'% Off';?>
    </span>
</span>

Its working perfect for Simple product but its not working for configurable products, I know configurable products doesn't have price but how can I get configurable first child product price or get price which is show as default price.

Best Answer

I added to the answer for others. Only note is, if you try getFinalPrice(); for simple product it will be same results with getSpecialPrice(); that reason I m checking type.

<span class="onsale">
<span>
<?php
    //echo $_product->getTypeId();
    if($_product->getTypeId()!="simple"){
        $label_price=$_product->getSpecialPrice();
        $final_price=$_product->getFinalPrice();
        $save_percent = 100 - round(($final_price / $label_price)*100);
        echo $save_percent.'% Off';
    }else{

        $label_price=$_product->getPrice();
        $final_price=$_product->getSpecialPrice();
        $save_percent = 100 - round(($final_price / $label_price)*100);
        echo $save_percent.'% Off';

    }
    ?>
</span>
</span>
Related Topic