1

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.

  • hope this will help you to get childs products price.. child products of configurable product – Mohammad Mujassam Dec 2 '16 at 4:52
  • I already read that one and tried but 'getUsedProducts($_product);' giving an error. That answer didnt work for me. – Whatatimetobealive Dec 2 '16 at 6:02
  • You can get $_product->getFinalPrice() for config product, You have got display price of product – Rakesh Jesadiya Dec 2 '16 at 6:32
  • Have you checked using i have mentioned method above? – Rakesh Jesadiya Dec 2 '16 at 6:53
  • Thank you man it worked but that one screwed simple product I solved it with basic if else. I ll add answer maybe someone else need it in future. – Whatatimetobealive Dec 2 '16 at 7:10
1

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>
  • For configurable product, which price it would get for final price – Jaisa Feb 20 '18 at 10:16
  • very bad answer, it not works – LucScu Jun 19 '18 at 15:37
0

I would suggest you use the

prcingHelper (Magento\Framework\Pricing\Helper\Data)

instead of calculating the price via

round () - 100 * 100 etc.

to prevent rounding errors.

-1

vendor\magento\module-configurable-product\Model\Product\Type\Configurable\Price.php

public function getPrice($product) { if ($product->getCustomOption('simple_product')) { return $product->getCustomOption('simple_product')->getProduct()->getFinalPrice(); } else { return $product->getFinalPrice();
} }

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.