Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kiennt2/6327881 to your computer and use it in GitHub Desktop.
Save kiennt2/6327881 to your computer and use it in GitHub Desktop.
Magento: Show price ranges of custom option prices in list.phtml instead of base price
<!-- code change to show price ranges -->
<?php
$product = Mage::getModel('catalog/product')->load($_product->getId());
$prodPrice = $product->getPrice();
if($product->getOptions()){
$minPrices=array();
$maxPrices=array();
foreach ($product->getOptions() as $_option) {
switch ($_option->getType()) {
case 'field': case 'file': case 'area': case 'date_time': case 'date': case 'time':
if($_option->getIsRequire()){
$minPrices[] = ($_option->getPriceType()=='percent') ? $prodPrice*$_option->getPrice()/100 : $_option->getPrice();
}
$maxPrices[] = ($_option->getPriceType()=='percent') ? $prodPrice*$_option->getPrice()/100 : $_option->getPrice();
break;
case 'radio': case 'drop_down':
$valuePrices=array();
foreach ($_option->getValues() as $_value){
$valuePrices[] = ($_value->getPriceType()=='percent') ? $prodPrice*$_value->getPrice()/100 : $_value->getPrice();
}
sort($valuePrices,SORT_NUMERIC);
if($_option->getIsRequire()){
$minPrices[] = $valuePrices[0];
}
$maxPrices[] = array_pop($valuePrices);
break;
case 'multiple': case 'checkbox':
$valuePrices=array();
foreach ($_option->getValues() as $_value){
$valuePrices[] = ($_value->getPriceType()=='percent') ? $prodPrice*$_value->getPrice()/100 : $_value->getPrice();
}
sort($valuePrices,SORT_NUMERIC);
if($_option->getIsRequire()){
$minPrices[] = $valuePrices[0];
}
$maxPrices[] = array_sum($valuePrices);
break;
}
}
$minTotal = $prodPrice + array_sum($minPrices);
$maxTotal = $prodPrice + array_sum($maxPrices);
if($minTotal==$maxTotal){
echo '<div class="price-box"><span class="regular-price">'.Mage::helper('core')->currency($minTotal).'</span></div>';
} else {
echo '<div class="price-box"><p class="price-from"><span class="price-label">From:</span>'.Mage::helper('core')->currency($minTotal).'</p><p class="price-to"><span class="price-label">To:</span>'.Mage::helper('core')->currency($maxTotal).'</p></div>';
}
} else {
echo $this->getPriceHtml($_product, true);
}
?>
<!-- end price range code -->
<?php //echo $this->getPriceHtml($_product, true) ?>
@manudev91
Copy link

very helpful..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment