Get the lowest and highest possible price for a configurable product
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;
A simpler one.
if($_product->isConfigurable()) // check if the product is configurable (u may check this in for loop of products)
{
//get associative (child) products
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$_product);
$childPriceLowest = "";
$childPriceHighest = "";
foreach($childProducts as $child){
$_child = Mage::getModel('catalog/product')->load($child->getId());
if($childPriceLowest == '' || $childPriceLowest > $_child->getPrice() )
$childPriceLowest = $_child->getPrice();
if($childPriceHighest == '' || $childPriceHighest < $_child->getPrice() )
$childPriceHighest = $_child->getPrice();
}
$price_array = array($childPriceLowest,$childPriceHighest); // array containing required values
}
This answer, also by @Marius, to a similar question is a good basis to work from. Using that, here's a solution to this problem that takes into account the potential for configurable products having multiple attributes that change price.
I've written it as function that takes a configurable product id, and returns a string of min to max price. It should be pretty clear how to work it into the context that you need.
function getPriceRange($productId) {
$max = '';
$min = '';
$pricesByAttributeValues = array();
$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
$basePrice = $product->getFinalPrice();
foreach ($attributes as $attribute){
$prices = $attribute->getPrices();
foreach ($prices as $price){
if ($price['is_percent']){ //if the price is specified in percents
$pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'] * $basePrice / 100;
}
else { //if the price is absolute value
$pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'];
}
}
}
$simple = $product->getTypeInstance()->getUsedProducts();
foreach ($simple as $sProduct){
$totalPrice = $basePrice;
foreach ($attributes as $attribute){
$value = $sProduct->getData($attribute->getProductAttribute()->getAttributeCode());
if (isset($pricesByAttributeValues[$value])){
$totalPrice += $pricesByAttributeValues[$value];
}
}
if(!$max || $totalPrice > $max)
$max = $totalPrice;
if(!$min || $totalPrice < $min)
$min = $totalPrice;
}
return "$min - $max";
}