如何在magento中获得可配置的产品价格范围?

时间:2013-04-02 05:57:26

标签: php magento magento-1.7

我需要配置产品价格范围,如

below image is my requirement

产品名称:$ 140 - 310我使用下面的代码

if(Mage::getSingleton('customer/session')->isLoggedIn())
{
        // Get group Id
        $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
}
else
{
        $groupId = 0;
}     
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $db->query('SELECT price ,final_price, min_price, max_price, tier_price, group_price FROM catalog_product_index_price WHERE entity_id='.$_product->getId().' AND customer_group_id ='.$groupId.' ORDER BY customer_group_id ASC LIMIT 1');
$rows = $result->fetch();

我还需要配置产品的常规价格范围。我也认为我的产品名称之后的范围是错误的,因为在Your Price have a price $135中我怎样才能获得最低价格和最高特价以及正常价格?

我怎么能得到它?

谢谢和问候

4 个答案:

答案 0 :(得分:4)

This answer关于Magento StackExchange的类似问题是一个很好的工作基础。使用它,这里是这个问题的解决方案,考虑到具有多个价格变化属性的可配置产品的潜力。

我把它写成了一个带有可配置产品ID的函数,并返回一个min到max的字符串。应该非常清楚如何将其应用到您需要的上下文中。

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";

}

答案 1 :(得分:2)

您可以先尝试获取该可配置产品的所有子产品,然后获取每个子产品的价格,并比较它们,找到最高和最低。

//load configurable product    
$product = Mage::getModel('catalog/product')->load(some_id);  
//load all children
$childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getUsedProducts(null,$product);   
foreach($childProducts as $child){
    $_child = Mage::getModel('catalog/product')->load($child->getId());
    $childPrice =  $_child->getPrice();
    //compare the $childPrice
}

答案 2 :(得分:1)

你可以使用这样的东西

$prices = array();
$associated = $_product->getTypeInstance(true)->getAssociatedProductCollection($_product)
->addAttributeToSelect('special_price');

foreach ($associated as $assoc) {
    $prices[] = $assoc->getSpecialPrice();
}
// calculate min max price here
if (count($prices)) {
    $min_price = min($prices);
    $max_price = max($prices);
} else {
    $min_price = 0;
    $max_price = 0;
}

也许不是完美的解决方案,但它有效

答案 3 :(得分:0)

尝试使用$product->getMinPrice()$product->getMaxPrice()

看一下app / code / core / Mage / Catalog / Model / Resource / Product / Collection.php