How to hide Price range for WooCommerce Variable Products? (with Video) Print

  • 22

Last updated - January 29, 2021

Every now and then we wish to tweak some of the features of WooCommerce. If you are selling variable products in your store, then you might have noticed the price range for the given product(s). While some of us seem fine with it, there are few who would want to hide the price range and show the product price when the respective variation is selected.

In this article, I’ll share code snippets that will help you alter the variable price range information for WooCommerce Variable Products.

Hide Price range for WooCommerce Variable Products

Following is a sample screenshot showing the default setup for WooCommerce variable products.

Hide Price Range for WC Variable Product | Default Variable price range setup
Default Variable price range setup

The Code snippet

Add the following code snippets at the end of the Themes function (functions.php) file of your currently activated website theme. You can find this file in Appearance > Editor > select functions.php file listed in the right sidebar menu.

//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(__('From: %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(__('From: %1$s','woocommerce')
                      , wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );

if ( $prod_price !== $regular_price ) {
$prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> <ins>' . 
                       $prod_price . $v_product->get_price_suffix() . '</ins>';
}
return $prod_price;
}

Thanks to our readers in the comment section who have helped us improve the code snippet. We have taken your valuable comments into consideration!

Here’s how the code snippet affects the variable product price range display.

Hide Price Range for WC Variable Product | Variation Price with Starting price specified
Variation Price with Starting price specified

Removing “From: $X”

While the code snippet given above in the article serves most of the purpose, you can choose to remove “From: $X” that specifies starting variation price as well.

To do this, add the following code snippet at the end of the functions.php file.

//Hide “From:$X”
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;
}

Following screenshot shows how the starting variation price can be removed by applying the above code snippet.

Hide Price Range for WC Variable Product | Variation Price without Starting variation price
Variation Price without Starting variation price

Therefore, in this way, you can hide Price range for WooCommerce Variable Products.

You can also watch a quick video tutorial to understand this process.


Was this answer helpful?

« Back