Easily change WooCommerce variable product prices to display the minimum price instead of the default price range displayed on both the product archive and single product pages, by using the woocommerce_variable_sale_price_html and woocommerce_variable_price_html filters.
/**
* Change variable product price to display From £#.## instead of price range
*
* @param string $price
* @param \WC_Product_Variable $product
*
* @return float|string
*/
function jc_variable_product_price_display($price, $product){
$price_min = $product->get_variation_price( 'min', true );
$price_sale_min = $product->get_variation_sale_price( 'min', true );
if($product->is_on_sale() && $price_min < $price_sale_min){
$price = sprintf('<del>%s</del><ins>%s</ins>', wc_price($price_min), wc_price($price_sale_min));
}else{
$price = wc_price($price_min);
}
return sprintf('From: %s', $price);
}
add_filter( 'woocommerce_variable_sale_price_html', 'jc_variable_product_price_display', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'jc_variable_product_price_display', 10, 2 );
Leave a Reply
Fields marked with an * are required to post a comment