jenny-r
8/14/2014 - 3:07 AM

WooCommerce filter for variations price range. Shows text of "Starting from" with minimum price instead of price range.

WooCommerce filter for variations price range. Shows text of "Starting from" with minimum price instead of price range.

add_filter( 'woocommerce_variable_price_html', 'djr_price_filter', 10, 2 );
add_filter( 'woocommerce_variable_sale_price_html', 'djr_price_filter', 10, 2 );
/**
 * Filters woocommerce_variable_price_html and woocommerce_variable_sale_price_html to apply text
 * of Starting from instead of the price range from - to that is the woocommerce default
 *
 * @param $price
 * @param $product
 *
 * @return string
 */
function djr_price_filter( $price, $product) {

	// Main price
	$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
	$price = $prices[0] !== $prices[1] ? sprintf( _x( 'Starting from %1$s', 'Price range: from', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

	// Sale
	$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
	sort( $prices );
	$saleprice = $prices[0] !== $prices[1] ? sprintf( _x( 'Starting from %1$s', 'Price range: from', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

	if ( $price !== $saleprice ) {
		$price = $product->get_price_html_from_to( $saleprice, $price ) . $product->get_price_suffix();
	} else {
		$price = $price . $product->get_price_suffix();
	}

	return $price;
}