Do you want to build loyalty among the best customers of your online store? How about the idea of calculating how much they have spent in your eCommerce to give them something as a gift when they reach a specific amount?
I propose the following…
Table of Contents
Create a discount coupon
It could be a coupon with these settings:
Of course, it’s just an idea, you can apply the discount you want, even make it usable as many times as you want, but I thought that a discount of 1 time per customer is a good incentive, but it’s you and your store.
Calculate customers’ cumulative spend and show them a banner when they reach a certain figure
The next thing is to create a function that calculates the accumulated spending of customers and, if it reaches the amount you indicate, show them a banner offering them to use the coupon we created before.
The code would look like this:
/* Show coupon to customers who accumulate more than 500 in purchases */
add_action( 'woocommerce_before_cart', 'show_banner_accumulated_spending_500' );
function show_banner_accumulated_spending_500() {
$current_user = wp_get_current_user();
// If not connected do not apply, can't calculate it
if ( 0 == $current_user->ID ) return;
// Show text banner above 500
if ( wc_get_customer_total_spent( $current_user->ID ) > 500 ) {
echo '<div class="woocommerce-info">¡Thank you for choosing us! - ¡You unlocked a PRO client discount! Use this coupon code <i><b>ZH47ZTEF</b></i> and save 5% of your next purchase.</div>';
}
}
Code language: PHP (php)
Some details of the code:
- Change the accumulated amount value (500) to the one you decide.
- Change the text to display in the banner and the coupon code to the one you have created.
- The code will only work for logged in users, who are logged in with their account to your online store, otherwise it is impossible to get their accumulated spend.
- The banner will only be displayed in the cart.
As always, you can add this code in 2 ways, you can choose:
- At the end of the functions.php file of the active (child) theme.
- To your customization plugin.
The result visible to the customer
When the customer has reached the accumulated amount in your store, when they visit the shopping cart to make their next purchase, they will see the banner, offering them the discount code.

Cool isn’t it?