Home » Php » php – How to get configurable product price range in magento?

php – How to get configurable product price range in magento?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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";

}

Answer:

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
}

Answer:

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

Answer:

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

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