WooCommerce – Show the smallest variation price with from X

/**
 * Shows only the smallest variation price.
 *
 * @see https://wpcodebook.com/woocommerce-show-the-smallest-variation-price-with-from-x/
 */

add_filter( 'woocommerce_variable_sale_price_html', 'wpcodebook_wc_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html',      'wpcodebook_wc_variation_price_format', 10, 2 );

function wpcodebook_wc_variation_price_format( $price, $product ) {

	// Main Price
	$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
	$price = $prices[0] !== $prices[1] ? __( 'from' ) . ' ' . wc_price( $prices[0] ) : wc_price( $prices[0] );

	// Sale Price
	$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
	sort( $prices );
	$saleprice = $prices[0] !== $prices[1] ? __( 'from' ) . ' ' . wc_price( $prices[0] ) : wc_price( $prices[0] );

	if ( $price !== $saleprice ) {
		$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
	}

	return $price;
}

4 thoughts on “WooCommerce – Show the smallest variation price with from X”

  1. Hi, thanks a lot for the code snippet above. It works great on the product pages after tweaking the code (as shown below) to show the prices as “Starting $9.99” for example.

    The only issue is, in the shop and category pages for on sale products it shows 3 lines. For example, “Starting at ,11.99 9.99 is show in 3 lines.
    Line 1: Starting at
    Line 2: $11.99 (which is formatted to show a strike out for the regular price)
    Line 3: $9.99 (regular price)

    Any idea how to fix this? Variable products not on sale display fine (i.e. Starting at $9.99 all in 1 line). This issue is only for shop, category and other pages where products are listed.

    Appreciate any help in tweaking the code. I am using Divi as my theme if that makes any difference and have added this code to functions.php in my child theme.

Leave a Comment