您好,我想显示产品的价格范围,如果有的话,只能通过自定义选项。我有这段代码,可以让我显示带有基本价格的价格,但如果产品有选项,则只显示选项价格,如果没有选项,则显示常规基本价格。
<!-- code change to show price ranges -->
<div class="price-box"><span class="regular-price">
<?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 Mage::helper('core')->currency($minTotal);
} else {
echo Mage::helper('core')->currency($minTotal).'-'.Mage::helper('core')->currency($maxTotal);
}
} else {
echo Mage::helper('core')->currency($prodPrice);
}
?>
</span></div>
<!-- end price range code -->