2013-04-02 47 views
7

我需要配置產品的價格範圍內像如何在magento中獲得可配置的產品價格範圍?

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所以我怎麼能得到最低價值和最大的特殊價格,也在正常價格?

我該怎麼做?

感謝和問候

+0

你找到一個堅實的答案這個問題? –

+0

仍然notif你有解決方案,而不是回答@Raphael Rafatpanah – Jalpesh

回答

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

也許不是完美的解決方案,但它工作

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

這假設底層簡單產品的價格是與可配置的這些選項的價格相同。不是一個安全的假設。 – Laizer

+0

這不太好,就像我們有100個可配置的產品一樣,每個頁面上都有10個簡單的子產品,那麼這會造成一個很大的性能問題。 100 * 10 = 1000個mysql查詢 – Saurabh

3

This answer到一個類似的問題關於Magento StackExchange是一個很好的工作基礎。使用這個,這個問題的解決方案考慮到具有多個價格變化屬性的可配置產品的潛力。

我已經將它寫爲一個函數,它需要一個可配置的產品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"; 

} 
0

嘗試使用$product->getMinPrice()$product->getMaxPrice()

看看應用程序/代碼/核心/法師/目錄/型號/資源/產品/ Collection.php

相關問題