Magento Lowest Price for a Configurable Product

back to tech articles
Magento 1.9 CE

Sometimes we want to display a price from field for Magento configurable products, advertising the lowest price for that product.

The challenge is that a configurable product is made up of the child products it contains, and we’ll need to check each child product’s price and compare them to get the lowest one. Hmm, we need a reusable function!

I find the best place for this type of method is a helper file. I’ve created an empty module for this, TC Myhelpers. I’m going to assume you can do that already and skip those steps. Let’s get straight to our method, and then we’ll see how we’re going to use it.

app/code/local/TC/Myhelpers/Helper/Data.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class TC_Myhelpers_Helper_Data extends Mage_Core_Helper_Data
{
    public function getLowestConfigPrice( $product ) {
        $childProducts = Mage::getSingleton('catalog/product_type_configurable')->getUsedProducts( null, $product );
        $childPriceLowest = '';

        if ( $childProducts ) {
            foreach ( $childProducts as $child ) {
                $_child = Mage::getSingleton('catalog/product')->load( $child->getId() );
                if ( $childPriceLowest == '' || $childPriceLowest > $_child->getPrice() ) {
                    $childPriceLowest =  $_child->getPrice();
                }
            }
        } else {
            $childPriceLowest = $product->getPrice();
        }

        return $childPriceLowest;
    }
}

Because our method is public, we can call it from inside any .phtml (template) file. All we need is a product object, and we’re ready to go. Here is a usage example.

app/design/frontend/default/mytheme/template/tc_myhelpers/pricefrom.phtml

1
2
3
4
5
6
7
<?php
// $prod is a product object, load as you wish
?>

    <?php if ( $prod->isConfigurable() ): ?>
        <?php $priceFrom = $this->helper('tc_myhelpers')->getLowestConfigPrice( $prod ); ?>
    <?php endif; ?>

And there you have it, price from, done.

It’s worth noting that our method is doing some heavy database interactions, even though we’re using Singletons, so it’s probably a good idea to keep calls to it on the low.

Leave a Reply

Your email address will not be published. Required fields are marked *