Home » WooCommerce: Get Total Spent By Customer

WooCommerce: Get Total Spent By Customer

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire, BusinessBloomer.com
 * @testedwith    WooCommerce 4.5
 * @donate $9     https://tutoraspire.com

add_action( 'woocommerce_before_cart', 'tutoraspire_show_banner_if_user_spent_more_than_99' );
function tutoraspire_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 '
Well done - you have unlocked your Valued Customer discount! Use coupon code JRP7EWKD2 and get 5% off all products.
'; } }

You may also like