If you sell a variable product on WooCommerce, it’s quite likely that different options come with different prices. In your store, customers can then choose a certain variation and will be charged the according price.
When selling many different options, in some cases store owners don’t want their customers to look at the full price spectrum when they get started. One reason could be, that customers sometimes don’t want to pay more, if they now the cheapest price from the start. Or customers shouldn’t be able to immediately compare their version to the highest price. Just as 2 possible examples.
To Remove the Price Range from being displayed, but still show the prices of Simple Products and Variable Products with the same price right underneath the title, you can add the following to your child themes function.php
//Remove Price Range
add_filter( 'woocommerce_variable_sale_price_html', 'detect_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'detect_variation_price_format', 10, 2 );
function detect_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
if ($prices[0] !== $prices[1]) {
$price = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
}
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
To this:
Additional helpful snippets:
Hide price range only on single product pages
If you still like to show the price range on archives and category pages, you can change the code below main price to:
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
if ($prices[0] !== $prices[1] && is_product()) {
$price = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
}
Move variant price above variations
You might want to keep the look and feel of all the other products and display the price above the variations. It might be, where your customers look first, as this is where the price usually displays. (Though keep in mind – if you have a long description and/or many variations options it could be too far from the add to cart button.)
If you do want to use it, add this code as well to your functions.php
//Move Variations price above variations to have the same template even if variations prices are the same
remove_action( ‘woocommerce_single_variation’, ‘woocommerce_single_variation’, 10 );
add_action( ‘woocommerce_before_variations_form’, ‘woocommerce_single_variation’, 10 );
Thank you mag for this snippet
hello
the code works fine but it removes the prices in home and shop pages i want to hide it only in product page
thank you
Hi!
You can wrap the code in an
if ( is_product() ) {
…
}
so it only gets triggered on single product pages, but not on any archives. That should do the trick.
Hope it helps!
hello Anna, with this code when all the variations are the same price without discounts it is not showing up the price at all as I think it uses the same position as range price, is it possible to correct it somehow?
also when I use if ( is_product() ) {
…
} range price appear again.
Ah, that’s true. You can change the line with $price in the //Main Price section to
if ($prices[0] !== $prices[1] ) {
$price = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
}
You can extend this condition with is_product() to only change the prices on the Single Product Pages
if ($prices[0] !== $prices[1] && is_product()) {
$price = $prices[0] !== $prices[1] ? sprintf( __( '', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
}
Hi, sorry but the above didn’t seem to help. if I put all variations at the same price price will not show up.
Also I noticed there was an issue when variations are same price – price appear on top of variations, when different – variations price appear below variations, so it created different templates and is difficult to use hooks such as after price, so here is revised code below.
The only question I have now, is it possible to add changed to range price only on product pages but keep in on loop category page? thank you!
//Move Variations price above variations to have the same template even if variations prices are the same
remove_action( ‘woocommerce_single_variation’, ‘woocommerce_single_variation’, 10 );
add_action( ‘woocommerce_before_variations_form’, ‘woocommerce_single_variation’, 10 );
//Remove Price Range
add_filter( ‘woocommerce_variable_sale_price_html’, ‘detect_variation_price_format’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘detect_variation_price_format’, 10, 2 );
function detect_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( ‘min’, true ), $product->get_variation_price( ‘max’, true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( ‘min’, true ), $product->get_variation_regular_price( ‘max’, true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = ” . $saleprice . ‘ ‘ . $price . ”;
}
return $price;
}
OK I think I figured out the code for what I want.
It shows range price on category and shows single prices for variable products without range price even if all variations are the same price.
Please see below, it works without sale price sections, so I deleted it
//Remove Price Range
add_filter( ‘woocommerce_variable_sale_price_html’, ‘detect_variation_price_format’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘detect_variation_price_format’, 10, 2 );
function detect_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( ‘min’, true ), $product->get_variation_price( ‘max’, true ) );
if ($prices[0] !== $prices[1] && is_product()) {
$price = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
}
return $price;
}
in addition it is useful
//Move Variations price above variations to have the same template even if variations prices are the same
remove_action( ‘woocommerce_single_variation’, ‘woocommerce_single_variation’, 10 );
add_action( ‘woocommerce_before_variations_form’, ‘woocommerce_single_variation’, 10 );
Thanks!
Above works best with this plugin to always show price on variable product page
https://ru.wordpress.org/plugins/force-default-variant-for-woocommerce/
So great you figured it out! I’ll add this to my article, so others can use it as well. And thanks for the tip with the default variant plugin! I’ll take a look at it 🙂
hey, thanks for sharing your code and help!
I tested the default variant plugin works but it not exactly as described as when “I choose default option for a particular product in product settings.
And set up rules in plugin settings, the default option is overwritten by the rules if it is different from what the rule says it shows default variation chosen but say still min rule price although the default variations is not min price, so when I click on it second time it only show correct price. So it basically shows the price based on the plugin settings and variations remain unchosen on first load, works very similar as show min/ max price of all variations” you can follow here if there will be any replies https://wordpress.org/support/topic/default-option-overwritten-buy-settings-rules/
in fact further test made me think that the default variant plugin is sort of fake, only shows price, as when product is out of stock the out of stock message is not loading on first load too( I found another code here, but it doesn’t seem to work, maybe you could review it would be helpful
https://gist.github.com/deepzak/35f579d6f9545e262d36
Hi Mag,
I don’t know the plugin and I don’t have the time right now to test it. But I hope the authors can help you out here!
alternatively, I reported it to woocommerce as well if you could think of how to change woocommerce code to show range price as here, when it is replaced by individual variation price, the it would work best
https://www.aliexpress.com/item/IF-ME-Vintage-Leaf-Feather-Multilayer-Leather-Bracelet-Men-Fashion-Braided-Handmade-Star-Rope-Wrap-Bracelets/32851204758.html
If this is what you need, you could try to
1. move the variant price above the short description with the code you already have
add_action( ‘woocommerce_before_variations_form’, ‘woocommerce_single_variation’, 10 );
2. Check via if a variation is selected
3. Remove the price range once 2. is true.
This isn’t tested and I can’t write the code for this at the moment. But maybe you can figure it out with these steps.
Hi there
I tried this out and it mostly worked but it results in ” being displayed on the front end. Is there a way to correct this?
Thanks
Adam
Hi Adam, do you have some more details and maybe a screenshot? There shouldn’t be any quotation marks in the frontend.
Hey Anna, thanks a lot for sharing this snippet ! I’m on WC 4.9.8 version currently and unfortunately, I have the same issue as Adam. I think I found out why it displays ” instead of pricing. It’s at the line
$price = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
in the //Main Price section.
When I replace the ” with ” characters it doesn’t display brackets in frond end anymore:
$price = $prices[0] !== $prices[1] ? sprintf( __( ”, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
So this is good on product page, but then again, if I go to archive/category page I have no price displayed at all on variation products. Do you have any idea on how to fix this ?
Thank you,
Maud
Hi Maud,
thank you for sharing! 🙂
What do you want to see on the archive pages? A from price or the price range?
Works perfectly!!
However, I got some errors when trying to add the code into child theme functions.php (this might be occurring only to me)
Turn out there’s some inconstant use of ” and ”
I changed the ” into two colons ” on line 9 and everything works perfectly!
Maybe you’ll want to change that letter in sprintf command in line 9
from
sprintf( __( ”, ‘woocommerce’ )
to
sprintf( __( ”, ‘woocommerce’ )
Thank you for the good work!
Thank you for the comment. Unfortunately, I’ve changed it quite a couple of times, but for some users when copying the code it still doesn’t work.
I’m glad it works for you now!
Nat, thanks for the tip. You pointed me in the right direction. For myself, I had to change 2 things on that line 9.
1st) The double quote looked weird on my screen (like a different font almost) so I replaced it with 2 single quotes like you suggested.
2nd) ‘woocommerce’ also had weird looking single quotes. I deleted them, and then typed my own and they looked normal after that.
Turns out the the world of quotes is far more complicated than it should be. http://unicode.org/cldr/utility/confusables.jsp?a=%22&r=None
Really, thank you so much!! The only answer I could find to this.
I have a minor issue with this code. All prices are now underlined, both on the shop page as the product page (the price for a product that’s on sale) and in the recently viewed widget in my sidebar as well. How do I change this?
Anything underlined is usually a CSS issue. Do you have a URL I can check?
Hi Anna, I already found a solution. I found another code and now everything looks fine.
Hi,
Could you please share that solution?
Thank you
this can help you:
https://es.wordpress.org/plugins/variation-prices-woocommerce/
Hello, Anna! Thanks for your code. I have a code in place to change variable price range to show as From: min price… so a bit different but hopefully you wont mind my questions since it´s a bit related with what some people are trying to accomplish here.
I stumbled upon your page while trying to hide this “From: Price” on product pages only. We want to be able to show it everywhere else on the store, which so far is working fine, but cant seem to be able to hide it on product page.
I tried using your if product_page code but it just wont hide it. Would you mind taking a look at the code? Please keep in mind “Desde: —” is “From: ” in spanish 🙂
//Hide Price Range for WooCommerce Variable Products
add_filter( ‘woocommerce_variable_sale_price_html’, ‘lw_variable_product_price’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘lw_variable_product_price’, 10, 2 );
function lw_variable_product_price( $v_price, $v_product ) {
// Product Price
$prod_prices = array( $v_product->get_variation_price( ‘min’, true ),
$v_product->get_variation_price( ‘max’, true ) );
$prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__(‘Desde: %1$s’, ‘woocommerce’),
wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
// Regular Price
$regular_prices = array( $v_product->get_variation_regular_price( ‘min’, true ),
$v_product->get_variation_regular_price( ‘max’, true ) );
sort( $regular_prices );
$regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__(‘Desde: %1$s’,’woocommerce’)
, wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
if ( $prod_price !== $regular_price ) {
$prod_price = ‘
‘.$regular_price.$v_product->get_price_suffix() . ‘‘ .$prod_price . $v_product->get_price_suffix() . ”;
}
return $prod_price;
}
//Hide “Desde:$X”
if ( is_product() ) {
add_filter(‘woocommerce_get_price_html’, ‘lw_hide_variation_price’, 10, 2);
function lw_hide_variation_price( $v_price, $v_product ) {
$v_product_types = array( ‘variable’);
if ( in_array ( $v_product->product_type, $v_product_types ) && !(is_shop()) ) {
return ”;
}
// return regular price
return $v_price;
}
}
Hi Cris,
if it’s not too late already, you do not need a second filter as in your //hide Desde part. Instead, you should include the is_product check in the lw_variable_product_price() function:
//Hide Price Range for WooCommerce Variable Products
add_filter( 'woocommerce_variable_sale_price_html', 'lw_variable_product_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'lw_variable_product_price', 10, 2 );
function lw_variable_product_price( $v_price, $v_product ) {
// Product Price
$prod_prices = array( $v_product->get_variation_price( 'min', true ),
$v_product->get_variation_price( 'max', true ) );
if ( is_product() ) {
$prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('', 'woocommerce'), wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
} else {
$prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('Desde: %1$s', 'woocommerce'), wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
}
// Regular Price
$regular_prices = array( $v_product->get_variation_regular_price( 'min', true ),
$v_product->get_variation_regular_price( 'max', true ) );
sort( $regular_prices );
if ( is_product() ) {
$regular_price = $regular_prices[0]!==$regular_prices[1] ? '' : wc_price( $regular_prices[0] );
} else {
$regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('Desde: %1$s','woocommerce'), wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
}
if ( $prod_price !== $regular_price ) {
$prod_price = '
'.$regular_price.$v_product->get_price_suffix() . '' .$prod_price . $v_product->get_price_suffix() . '';
}
return $prod_price;
}
//Hide “Desde:$X”
// add_filter('woocommerce_get_price_html', 'lw_hide_variation_price', 10, 2);
function lw_hide_variation_price( $v_price, $v_product ) {
if ( is_product() ) {
$v_product_types = array( 'variable');
if ( in_array ( $v_product->product_type, $v_product_types ) && !(is_shop()) ) {
return '';
}
// return regular price
return $v_price;
}
}
Hi i tried all of that codes, but nothing worked, simply i want on a shop catalog page , and everywhere on site to see only one price, only in single product when chose the variable price to change the price .
https://prnt.sc/CF4MV4VJryA-