public
Last active

WooCommerce - Show min - max prices for variations, rather than the min price only

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
* 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 .= '<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);
}
 
return $price;
}

Is there any way to display a specific variation price instead of the min/max price? For instance I have three variations attached to each product, could I specify one to display?

Hello Mike Jolley. I'm terrible at coding. I need your help, though not too much of it as I suspect it will be easy for your powerful brain.

All I want to do is display ONLY the max_variable_price. Can this filter be amended to do that? I was going to use CSS to display:none using this exact filter but both the min and max values have the same class.

I'd be over the moon if you could help.

@morgyface you just need the:

$price .= woocommerce_price($product->max_variation_price);

part then. Replace lines 10-18 within the function.

@mikejolley

If I do as suggested, it removes the option to select between different variables (in my case different price options) on the product. Do you have any suggestion to fix that?

Thanks

Very useful but doesn't work if product has sale price.

Hi. What if you want to remove the "From" option on the product page all together?

Yes, I am also wondering where the "From" is being generated from. Of course, we can always use CSS to hide it, but you would think it would be fairly easy to find the PHP file generating it and remove it. I have search for hours and with no luck!

By the way, excellent work!

Hi, Mike,

I'm wondering if this code will help me at all
I've added postage options in my variables, so the price shows as from Min variable price.
is there any way to make it show a specific variable e.g. if you look at this shop page :
http://www.buyflowersnow.com.au/product/designer-choice-arrangements/
the price without any extras, or postage is $75, this shows if you select 'no thank you' for the extras, but not initially.
i was hoping to keep the postage options there as woocommerce isn't being very friendly with displaying them on checkout. my only option at the moment is to remove them.

@angryoaf If you want it to do both non-sale and sale items, add a second add_filter() with this:

add_filter('woocommerce_variable_sale_price_html', 'custom_variation_price', 10, 2);

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?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.