How to hide Price range for WooCommerce Variable Products?

0
2082

Last updated - May 10, 2018

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.

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 ) {

// Regular Price
$v_prices = array( $v_product->get_variation_price( 'min', true ), 
                            $v_product->get_variation_price( 'max', true ) );
$v_price = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s', 'woocommerce'), 
                       wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );

// Sale Price
$v_prices = array( $v_product->get_variation_regular_price( 'min', true ), 
                          $v_product->get_variation_regular_price( 'max', true ) );
sort( $v_prices );
$v_saleprice = $v_prices[0]!==$v_prices[1] ? sprintf(__('From: %1$s','woocommerce')
                      , wc_price( $v_prices[0] ) ) : wc_price( $v_prices[0] );

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

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 ) ) { 
       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.

 

Check out other articles for WooCommerce customization here.

Or continue exploring LearnWoo for amazing articles.


Suggested reading:

WooCommerce Product Variations – How to move Add to Cart button above Product short description?