Enabling WooCommerce functionalities only if a “user spent more than X” is not unusual. For example, you may want to display banners, special offers, discounted prices, conditional content to customers who have purchased more than a given dollar threshold.

While coding a function that could get the total spent by a user ID, I stumbled upon a WooCommerce function that already achieves that, out of the box: wc_get_customer_total_spent( $user_id ).

You can use it as a conditional tag and run a function only when such threshold is reached. So, let’s see how to use it. Enjoy!

This cart banner will only appear if the logged in user has spent more than $99. This is awesome for running special promotions and segmenting your audience.

PHP Snippet: Display Banner Only If User Spent More Than $99 @ WooCommerce Cart

/**
 * @snippet       Banner Based On Total Spent @ WooCommerce Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, BusinessBloomer.com
 * @testedwith    WooCommerce 4.5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_cart', 'bbloomer_show_banner_if_user_spent_more_than_99' );
  
function bbloomer_show_banner_if_user_spent_more_than_99() {
	
	$current_user = wp_get_current_user();
	
	// if logged out, exit
	if ( 0 == $current_user->ID ) return;
	
	// if spent more than 99, display banner
	if ( wc_get_customer_total_spent( $current_user->ID ) > 99 ) {
		echo '<div class="woocommerce-info">Well done - you have unlocked your Valued Customer discount! Use coupon code <i><b>JRP7EWKD2</b></i> and get 5% off all products.</div>';
	}
	
}
Share:

Leave a Reply