Displaying one price for variable products

To display variable product price as a single value, add the following code to the functions.php file in theme.  After adding the code, the price of the cheapest variant with the prefix “From” will be presented on all pages except the product page.

Several modifications can be made to the code:

  1.  In order for the price in this form to be displayed on every subpage, the condition if ( !is_singular( 'product' ) ) { should be deleted: lines 3 and 18 should be removed.
  2.  You can change the content of the prefix and optionally add the content of the suffix: lines 8 and 9
  3.  As the displayed price, you can use the maximum price or one of the regular prices: on line 16, change $min_price to the required price.
add_filter( 'woocommerce_variable_price_html', 'studiowp_woocommerce_variable_price_html', 10, 2 );
function studiowp_woocommerce_variable_price_html( $price, $product ) {
	if ( !is_singular( 'product' ) ) {
		$prices = $product->get_variation_prices( true );
		if ( empty( $prices['price'] ) ) {
			$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
		} else {
			$prefix = 'From ';
			$suffix = '';

			$min_price = current( $prices['price'] );
			$max_price     = end( $prices['price'] );
			$min_reg_price = current( $prices['regular_price'] );
			$max_reg_price = end( $prices['regular_price'] );

			$price = wc_price( $min_price );
			$price = $prefix . $price . $suffix;
		}
	}
	return $price;
}

View the code on Gist.

If you do not use the child theme, remember that changes made to the functions.php file will be overwritten when the theme is updated.

While copying code to functions.php file it is not nessesary to copy <?php opening tag.

About grola

Passionate about Wordpress and WooCommerce for many years. Author of plugins and short snippets improving Wordpress and WooCommerce.

View all posts by grola →