Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get configurable product price range in magento?

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

like image 891
Jalpesh Patel Avatar asked Apr 02 '13 05:04

Jalpesh Patel


4 Answers

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

}
like image 167
Laizer Avatar answered Oct 19 '22 20:10

Laizer


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
}
like image 2
JuLy Avatar answered Oct 19 '22 20:10

JuLy


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

like image 2
TaganPablo Avatar answered Oct 19 '22 19:10

TaganPablo


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

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

like image 1
Renon Stewart Avatar answered Oct 19 '22 21:10

Renon Stewart