Magento – How to Display available Lowest price for Magento configurable products

configurable-productmagento-1.9price

We know that configurable products has different prices. By default Magento displays base price of the configurable product. But I want to display the lowest price available in associated products of that configurable product everywhere in the store. How can I do that?I use Magento 1.9.0.1

Best Answer

The price is pulled from catalog/product/price.phtml. The following code will get the lowest price of all associated simple products. This doesn't take in to consideration all of the tax rules in this file though. You could also change the variable $finalPrice in the below code to just be 0 and that would return the smallest amount that would be added on to the configurable.

// Load the full product
$configProduct = Mage::getModel('catalog/product')->load($_product->getId());
// Full product has to be loaded in order to pull configurable attributes
$configAttributes = $configProduct->getTypeInstance(true)->getConfigurableAttributes($configProduct);

// Get the configurable base price
$basePrice = $configProduct->getFinalPrice();
// Create an array to store attribute price values
$attributePrices = array();

// Loop through the configurable attributes
foreach ($configAttributes as $configAttribute) {
    // Grab the pricing information for the configurable attributes
    $simplePrices = $configAttribute->getPrices();
    // Loop through the pricing information for the configurable attributes
    foreach ($simplePrices as $simplePrice) {
        // Check if the additional price is percent or fixed of configurable price
        if ($simplePrice['is_percent']) /* Percent */ {
            // Calculate percentage using base price of configurable product
            $attributePrices[$simplePrice['value_index']] = ((float)$simplePrice['pricing_value'] * $basePrice) / 100;
        } else /* Fixed */ {
            $attributePrices[$simplePrice['value_index']] = (float)$simplePrice['pricing_value'];
        }
    }
}

// Create a array of all simple products associated to the configurable product
$simpleProducts = $configProduct->getTypeInstance()->getUsedProducts();
// Create an array to store simple price final price
$finalPrices = array();

// Loop through all the simple products
foreach ($simpleProducts as $simpleProduct) {
    // Create final price variable and set it to base configurable value
    $finalPrice = $basePrice;
    // Loop through the configurable attributes
    foreach ($configAttributes as $configAttribute) {
        // Get the value of the attribute for the simple product
        $attributeValue = $simpleProduct->getData($configAttribute->getProductAttribute()->getAttributeCode());
        // Get the price of the attribute from the price array
        if (isset($attributePrices[$attributeValue])) {
            // Add the value onto the final price of the simple product
            $finalPrice = $finalPrice + $attributePrices[$attributeValue];
        }
    }

    // Check if simple product is saleable before adding final price to our array
    if ($simpleProduct->isSalable()) {
        // Add the final price to the final prices array
        $finalPrices[$simpleProduct['entity_id']] = $finalPrice;
    }
}

// Gets minimum price of the array 
echo min($finalPrices);
Related Topic