Last active
September 28, 2016 13:01
-
-
Save octoxan/f1c54923322a8fc6b73c3a09812ed373 to your computer and use it in GitHub Desktop.
Magento 1.x getPriceRange Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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