WooCommerce Get Total Spent By Customer

WooCommerce Get Total Spent By CustomerDo you want to get the total spent by a customer and offer them a discount? Then read on, as this post aims to provide you with a custom code snippet to help you get the total amount spent and give them a special offer or display a banner.

WooCommerce powers many online stores today because it is flexible to customization. It is very easy to add your own functionality using extensions or custom code snippets.

One way of enticing customers in your store is by giving them discounts after spending a certain amount. Customers will definitely make repeat purchases to get a discount. This is a great way to increase your revenue. This is one of the popular strategies used by most online stores today.

If you do a quick search on the internet, you will find many plugins that will help you achieve this. However, it is worth mentioning that if you have many plugins, they may affect the loading speed of your site. This is why we recommend using code snippets.

If you are a beginner, you may have a hard time implementing this on your site. This is why we created this step by step guide to help you out.

WooCommerce Get Total Spent By Customer

By the end of this post, you will be to get the total amount spent by the customer and display a banner with a special offer. However, the customer must spend a certain amount to display the banner on the cart page.

It is worth mentioning that we will be modifying some of WooCommerce’s core files. This means that you need to install or create a child theme to ensure your changes are not lost during an update.

With that in mind, let us get right into it.

Steps to Get Total Spent By Customer

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to get the total spent by the user.
  3. Add the following code to the functions.php file:
[php] add_action( ‘woocommerce_before_cart’, ‘njengah_show_banner_if_user_spent_more_than_99’ );
function njengah_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> ERTP7EDF2</b></i> and get 5% off all products.</div>’;
}
}

[/php]
  1. This is the outcome:coupon

Wrapping Up

In today’s post, we have shown you how to get the total spent by the customer and display a banner on the cart page. The code snippet gets the total spent by a user ID, using the wc_get_customer_total_spent( $user_id ) function. We have used it as a conditional tag, and it only runs when the threshold is achieved.

We hope that this post provided you with the solution you were looking for.

Similar Articles