2

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.

5
  • hope this will help you to get childs products price.. child products of configurable product Dec 2, 2016 at 4:52
  • I already read that one and tried but 'getUsedProducts($_product);' giving an error. That answer didnt work for me. Dec 2, 2016 at 6:02
  • You can get $_product->getFinalPrice() for config product, You have got display price of product Dec 2, 2016 at 6:32
  • Have you checked using i have mentioned method above? Dec 2, 2016 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. Dec 2, 2016 at 7:10

4 Answers 4

2

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

Head to this answer, which explains how to get the pricing of all types of products in Magento.

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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.