Change price format in WooCommerce variable products

In WooCommerce the format of variable product prices can be a bit confusing.

The highest and lowest prices are separated by a hyphen as follows:

woocommerce pricing format

This way of displaying prices on variable products is not the most intuitive in the world.

In this tutorial we will see how to change it so that instead of appearing as a price range it appears "From €35"

Being the result:

price from woocommerce

Subscribe to my channel:  

To achieve this we have to add the following code in the functions.php file of our template.

Remember that if you are going to make changes to the template files, it is advisable to use a child theme and of course make a backup of your website 😉

/**
 * Change price format from range to "From:"
 *
 * @param float $price
 * @param obj $product
 * @return str
 */
function iconic_variable_price_format( $price, $product ) {
 
    $prefix = sprintf('%s: ', __('Desde', 'iconic'));
 
    $min_price_regular = $product->get_variation_regular_price( 'min', true );
    $min_price_sale    = $product->get_variation_sale_price( 'min', true );
    $max_price = $product->get_variation_price( 'max', true );
    $min_price = $product->get_variation_price( 'min', true );
 
    $price = ( $min_price_sale == $min_price_regular ) ?
        wc_price( $min_price_regular ) :
        '<del>' . wc_price( $min_price_regular ) . '</del>' . '<ins>' . wc_price( $min_price_sale ) . '</ins>';
 
    return ( $min_price == $max_price ) ?
        $price :
        sprintf('%s%s', $prefix, $price);
 
}
 
add_filter( 'woocommerce_variable_sale_price_html', 'iconic_variable_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'iconic_variable_price_format', 10, 2 );

You can change the word "From" to another in the variable $prefix = »;

I hope it will be useful to you 😉
regards
Oscar

Leave a comment