Do 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:
- Log into your WordPress site and access the Dashboard as the admin user.
- 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.
- Add the following code to the functions.php file:
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]
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
- How to Force Maximum 1 Category Per Product WooCommerce
- How to Customize WooCommerce Currency [Code Snippets]
- How to Upload Image On Product Page In WooCommerce
- How to Set Number of Products Per Page WooCommerce
- How to Get Current WooCommerce Product Category
- How to Get WooCommerce Current Order ID from URL
- How to Set Up WooCommerce Cart and Checkout on the Same Page
- WooCommerce Extra Charges To WooCommerce Checkout
- How to Change Cart Subtotal WooCommerce
- How to Add WooCommerce Shipping Calculator on Cart Page
- How to Add Continue Shopping Button WooCommerce Checkout
- How to Add Product to Cart Programmatically in WooCommerce
- How to Get Post ID by Slug in WordPress With a Practical Example
- How to Add WooCommerce Shopping Cart Icon In Menu
- How To Set Up Cart Page WooCommerce
- How to Add Information to the WooCommerce Shop Page
- How to Show Regular and Sale Price WooCommerce
- How to Hide Out of Stock Products in WooCommerce
- How to Add WooCommerce Shipping Calculator on Checkout Page
- How to Get Current Product ID WooCommerce