Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created August 14, 2014 03:07
Show Gist options
  • Save djrmom/c736071a2d6f016a6a30 to your computer and use it in GitHub Desktop.
Save djrmom/c736071a2d6f016a6a30 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment