Magento – Get the lowest and highest possible price for a configurable product

configurable-productproduct

What would be a good way of retrieving the lowest and highest possible prices for a given configurable product – based on its availble options?

For example a t-shirt comes in the following sizes and prices:

Small - $10
Medium - $20
Large - $30

I want to get an array like so:

array(10, 30)

My best idea so far is load the configurable product type instance and use getUsedProducts, then create an array of prices, sort and slice.

That should work, however this needs to be run on the product list template so it needs to be semi efficient.

Any others out there faced this problem before?

EDIT – that wont work since i want the configurable pricing value i.e. the additive price magento puts on top of the configurable product price

Best Answer

Try this approach. use the config array that the attribute dropdowns use to change the price of the configurable product. Let's assume that $productId is the ID of the configurable product.

$product = Mage::getModel('catalog/product')->load($productId);
$block = Mage::app()->getLayout()->createBlock('catalog/product_view_type_configurable');
$block->setProduct($product);
$config = json_decode($block->getJsonConfig(), true);
$basePrice = $config['basePrice'];
$min = null;
$max = null;
foreach ($config['attributes'] as $aId=>$aValues){
    foreach ($aValues['options'] as $key=>$value){
        if (is_null($min) || $min>$value['price']){
            $min = $value['price'];
        }
        if (is_null($max) || $max<$value['price']){
            $max = $value['price'];
        }
    }
}
//until here we have the min and max price differences. Now just add the base price.
$min += $basePrice;
$max += $basePrice;
Related Topic