Menu Close

WooCommerce: Replace Variable Price With Active Variation Price

default

Surprisingly enough, variable products with a price range display two prices: at the top right you find the “parent” product price, displayed as a range; but once you select a variation, a second price appear just above the variation add to cart. Somewhat confusing.

In today’s solution, we’ll see once and for all how to replace the top right variable product price with the one of the current variation, while also hiding the variation price. Therefore, you’ll see a single price on the single product page for variable products. Enjoy!

Here’s the default display when a variable product has a price range: on top there is the “static” price for all variations;

PHP Snippet: Dynamically Update Variable Product Price With Current Variation Price

Note: the jQuery statements included in the snippet target specific CSS classes in order to replace the price HTML. If your theme uses customized HTML and different classes, the code won’t work. Make sure to test this code on a default theme, such as Storefront, to make sure it actually works; and only then try to adjust the jQuery to target the correct non-default classes.

/**
* @snippet       Replace Variable Price With Variation Price | WooCommerce
* @how-to        Get CustomizeWoo.com FREE
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 7
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/

add_action( 'woocommerce_variable_add_to_cart', 'bbloomer_update_price_with_variation_price' );
 
function bbloomer_update_price_with_variation_price() {
	global $product;
	$price = $product->get_price_html();
	wc_enqueue_js( "		
		$(document).on('found_variation', 'form.cart', function( event, variation ) {   
			if(variation.price_html) $('.summary > p.price').html(variation.price_html);
			$('.woocommerce-variation-price').hide();
		});
		$(document).on('hide_variation', 'form.cart', function( event, variation ) {   
			$('.summary > p.price').html('" . $price . "');
		});
   " );
}

Screenshot after the snippet is installed and a variation is selected:

Once the snippet above is active, there is only 1 price! If a variation is active, the variation price will show at the top – if no variation is found or after clicking the “clear” button, the price will go back to the one of the parent variable product

Mini-Plugin: Business Bloomer WooCommerce Replace Variable Price With Active Variation Price

You don’t feel confident with coding? You need a simple solution for improving the variable product page UX? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer Replace Variable Price With Active Variation Price is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. A single and easy admin dashboard.

Screenshot of the settings? Here you go:

Quick demo? Here it is:

As you can see the settings are pretty straight forward – I mean, you may not need to touch any settings whatsoever as usually the plugin works straight away. Awesome!

View Source
Posted in WooCommerce Tips