I want to display the price range for configurable products on the product page.
Magento – Magento 2 display configurable product Price Range in product page
configurable-productmagento2price
Related Solutions
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);
Ideally, you don't want to have price '0' on the configurable product. It should have a base price, and then additional options may cost more.
If your pricing model is made so that different options will always have a different price, you may want to look at Simple Configurable Products - this extension will use the prices on the Simple products everywhere. It might be easier to manage for you than Magento's default implementation.
Best Answer
Please follow the below steps.
Step 1: Create a layout file
catalog_product_view_type_configurable.xml
on this pathapp/design/frontend/{{companyname}}/{{themename}}/Magento_ConfigurableProduct/layout/catalog_product_view_type_configurable.xml
Step 2: Create template file
price_range.phtml
on this pathapp/design/frontend/{{companyname}}/{{themename}}/Magento_ConfigurableProduct/templates/product/price_range.phtml