Skip to content

Instantly share code, notes, and snippets.

@angryoaf
Last active July 3, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angryoaf/5380297 to your computer and use it in GitHub Desktop.
Save angryoaf/5380297 to your computer and use it in GitHub Desktop.
WooCommerce - Show min - max prices for variations, rather than the min price only. | Also works if products have sale price applied.
/**
* This code should be added to functions.php of your theme
**/
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$price = '';
if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<div class="lbwWooVariaPrice"> <span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
if ( $product->max_variation_price && $product->max_variation_price !== $product->min_variation_price ) {
$price .= '<span class="to"> ' . _x('to', 'max_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->max_variation_price) . '</div>';
}
return $price;
}
// Products variations with sale price
add_filter('woocommerce_variable_sale_price_html', 'custom_variation_sale_price' , 10, 2);
function custom_variation_sale_price( $price, $product ) {
$price = '';
if ( !$product->min_variation_sale_price || $product->min_variation_sale_price !== $product->max_variation_sale_price ) $price .= '<div class="lbwWooVariaPrice"> <span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
if ( $product->max_variation_sale_price && $product->max_variation_sale_price !== $product->min_variation_sale_price ) {
$price .= '<span class="to"> ' . _x('to', 'max_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->max_variation_sale_price) . '</div>';
}
return $price;
}
@angryoaf
Copy link
Author

Also: Added div with class selector "lbwWooVariaPrice" from - to price can be easily targeted and styled.
I chose not to show crossed out prices because I think it takes up too much room.

disclaimer I am not a programmer! I made this fork because the original did not work when products had a sale price applied.

@sngliwei
Copy link

Thanks for this, I had the exact same issue and this solves it.

@ancaemcken
Copy link

Great snippet :) It was very helpful in getting the "From: min_price" back after 2.1.1. Do you happen to have a similar solution for Product Bundles?

@queenb123
Copy link

This does not work for me i am getting a syntax error can anyone help me please, i am not good at coding, i have copied and pasted the above code into my functions.php (not the child theme tried that and it did not work),i really need help what do i do please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment