How to add a string like "Price from" next to configurable product prices on Category, Search and Product pages, but only if the configurable product has simple products with different prices in it?
Add ‘Price From’ to Configurable Products with Different Prices in Magento 2
configurable-productmagento2
Best Answer
I did this by extending
Magento\ConfigurableProduct\Pricing\Price\FinalPrice
with functions for getting min/max final price of configurable products and inserting the label only when needed (when there are simple products with different prices inside the configurable).Gonna post my solution for others that might need help with this:
Then you can call the new function
getMinFinalAmount
andgetMaxFinalAmount
from your custom:And display the label where you want like this:
All that's left is to use JS to hide the
Price from
label when a simple configuration is selected.That depends on if you're using swatches or not.
To remove the label by using swatches, you need to edit
Magento_Swatches/web/js/swatch-renderer.js
.I added this code to the
_UpdatePrice
function which is executed when a swatch is clicked.$product.find('.price-label').hide(0);
To remove the label by using ordinary configurable options, you need to edit
Magento_ConfigurableProduct/web/js/configurable.js
.I added this code to the
_calculatePrice
function which is executed when a configurable option is selected from the dropdown.$(this.options.priceHolderSelector).find('.price-label').hide(0);