Skip to content

Instantly share code, notes, and snippets.

@octoxan
Last active September 28, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octoxan/f1c54923322a8fc6b73c3a09812ed373 to your computer and use it in GitHub Desktop.
Save octoxan/f1c54923322a8fc6b73c3a09812ed373 to your computer and use it in GitHub Desktop.
Magento 1.x getPriceRange Function
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;
}
if ($min == $max) {
return "$".$min;
} else {
return "$"."$min - "."$"."$max";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment