Answers:
试试这种方法。使用属性下拉列表用来更改可配置产品价格的配置数组。假设这$productId
是可配置产品的ID。
$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;
一个简单的。
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
}
@Marius对此类似问题的答案也是一个很好的依据。使用该方法,这里是针对此问题的解决方案,其中考虑到具有多个可改变价格的属性的可配置产品的潜力。
我已将其编写为带有可配置产品ID的函数,并返回最小到最大价格的字符串。应该很清楚如何在所需的上下文中使用它。
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";
}
尽管这不是最好的例子,但这可以解决问题
<?php $_configurable = $_product->getTypeInstance()->getUsedProductIds(); ?>
<?php $price_array = array(); $i=0; ?>
<?php foreach ($_configurable as $_config): ?>
<?php $_simpleproduct = Mage::getModel('catalog/product')->load($_config); ?>
<?php array_push($price_array, $_simpleproduct->getPrice()); ?>
<?php $i++; endforeach; ?>
<?php if(min($price_array) != max($price_array)): ?>
<div class="price-box">
<span id="product-price-<?php echo $_product->getId(); ?>" class="regular-price">
<?php echo Mage::helper('core')->currency(min($price_array)); ?>
<span class="price" style="padding: 0 5px; color: black;">-</span>
<span class="final"><?php echo Mage::helper('core')->currency(max($price_array)); ?></span>
</span>
</div>
<?php else: ?>
<?php echo $this->getPriceHtml($_product, true); ?>
<?php endif; ?>
使用EE 1.14,由于某种原因,使用上述方法获得了“有趣”的数字。在$childPriceLowest
与$childPriceHighest
不回真正的最小值和最大值所有的时间。有时,具有多个配置选项等。我看到了中间值。
我最终使用了这个:
//get associated (child) products
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$_product);
//cycle through child products, dump prices in an array....
foreach($childProducts as $child){
$_child = Mage::getModel('catalog/product')->load($child->getId());
$childPrices[] = $_child->getPrice();
}
// concentrate the array to unique values and sort ascending....
$childPrices = array_unique($childPrices, SORT_NUMERIC);
sort($childPrices); // array containing required values
然后,稍后以回显范围:
<?php echo Mage::helper('core')->currency( $childPrices[0], true, false); ?>
<span class="config-price-divider"> - </span>
<?php echo Mage::helper('core')->currency( end($childPrices), true, false)?>
(例如“ $ 10.00-$ 30.00”)
您还可以尝试使用此简单代码,根据需要获取最高价/最低价
$childPriceHighest = '';
$product = Mage::getModel('catalog/product')->load($productId);
$childProducts = Mage::getSingleton('catalog/product_type_configurable')->getUsedProducts( null, $product );
if ($childProducts) {
foreach ($childProducts as $child) {
$_child = Mage::getSingleton('catalog/product')->load($child->getId());
if ($childPriceHighest == '' || $childPriceHighest < $_child->getPrice()) {
$childPriceHighest = $_child->getPrice();
}
}
} else {
$childPriceHighest = $product->getPrice();
}
return $childPriceHighest;