How to get configurable product price range in magento?
Asked Answered
I

4

8

I need configure Product price range like

below image is my requirement

For the product name: $140 - 310 i use below code

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();

i also need a regular price range for the configure product. i also think that my range after product name my be wrong because in Your Price have a price $135 so how can i get minimum value and maximum special price and also in regular price?

How can i get that?

Thanks and Regards

Inefficiency answered 2/4, 2013 at 5:57 Comment(2)
Have you found a solid answer to this issue?Oleate
still notif you have solution than post it in answer @Raphael RafatpanahInefficiency
Q
4

This answer to a similar question on the Magento StackExchange is a good basis to work from. Using that, here's a solution to this problem that takes into account the potential for configurable products having more than one price-changing attribute.

I've written it as function that takes a configurable product id, and returns a string of min to max price. It should be pretty clear how to work it into the context that you need.

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

}
Quicksilver answered 2/3, 2014 at 20:46 Comment(2)
There is a bug when using your code in combination with multiple attributes (e.g. the first attribute is 'fix' + second is 'percentage').Paba
Try this: foreach ($simple as $s){ $totalPrice = $product->getFinalPrice(); foreach ($attributes as $attr){ $val = $s->getData($attr->getProductAttribute()->getAttributeCode()); foreach ($attr->getPrices() as $price){ if ($price['value_index'] != $val) continue; if ($price['is_percent']) $totalPrice = $price['pricing_value'] * $totalPrice/100; else $totalPrice += $price['pricing_value']; } } }Paba
S
2

You can use something like this

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

Maybe not perfect solution, but it works

Selfpollination answered 2/4, 2013 at 8:45 Comment(0)
C
2

can u try to get all the child products of that configurable product first, then get the price of each child product, and compare them, find the highest and the lowest.

//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
}
Consult answered 2/4, 2013 at 11:0 Comment(2)
This assumes that the price of the underlying simple products are the same as the price of those options in the configurable. Not a safe assumption.Quicksilver
this is not good as well like we have 100 configurable product and each have 10 simple child product on each page then It is going to create a big performance issue. 100 * 10 = 1000 mysql queriesGeralyngeraniaceous
C
1

Try using the $product->getMinPrice() and $product->getMaxPrice()

Take a look at app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

Cargian answered 20/12, 2015 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.