Discounts and coupons are great tools to motivate your customers to buy more of your products. By default, if you want to give specific customers a discount, you need to provide them with a coupon code. This process is absolutely fine, but the more seamless buying experience you create, the more customers will get back to you. Why not auto-apply discounts for your customers?
Maybe you want to retain your regular customers. Or perhaps you want to provide automatic discounts for users based on their user roles. Anyways, there’re multiple ways to auto-apply discounts in WooCommerce. Today, I want to tell you about the pros and cons of methods I know.
Introduction
If you have already read my previous posts, you might have noticed that I love introducing my tutorials with a realistic but hypothetical case we need to solve. This time, we’ll have the following conditions:
- We want to retain our regular customers, so we need to motivate them keep using our store.
- To do this, we’re going to auto-apply discounts for users that have bought products for at least $1000 in total.
Now, understanding the goal, let’s see how we can achieve this using PHP and in-build WordPress and WooCommerce features.
How to get a user’s total spent in WooCommerce?
First of all, we need to see if a user meets the requirements for an automatic discount. In other words, we need to get a user’s total spent and check if this number is greater than or equal to 1000. Let’s thank the WooCommerce developers for creating a built-in feature allowing us to easily get a user’s total spent. All we need is to have access to the current user’s ID.
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$total_spent = floatval( $customer->get_total_spent() );
If a user hasn’t spent a penny on your website yet, this function will return 0.
How to auto-apply a coupon programmatically?
First of all, we need to create a coupon we’re going to auto-apply. I’ll create a coupon with the following options:
- Discount type: Percentage discount
- Coupon amount: 5
- Allow free shipping: true
The coupon’s code is AUTOCOUPON.
You can also limit your coupon by specific products or product categories.
After you create a coupon, we have to write some custom code to auto-apply this coupon for all of our regular customers. Here’s how you can do it:
add_action( 'woocommerce_before_calculate_totals', 'auto_apply_coupon_for_regular_customers', 10, 1 );
function auto_apply_coupon_for_regular_customers( $cart ) {
// We should apply the coupon for the authorized regular customers only
if ( ! is_user_logged_in() ) {
return;
}
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$total_spent = floatval( $customer->get_total_spent() );
if ( $total_spent < 1000 ) {
return;
}
// Let's check if a user has already applied any coupons
$applied_coupons = WC()->cart->get_applied_coupons();
if ( ! empty( $applied_coupons ) ) {
foreach ( $applied_coupons as $code ) {
$coupon = new WC_Coupon( $code );
$individual_use = $coupon->get_individual_use();
// If a user already has already applied a coupon which cannot be used with other coupons, we don't want to disturbe a user with an error message
if ( $individual_use ) {
return;
}
}
}
// Change this variable's value to the coupon code you just created
$coupon_code = 'AUTOCOUPON';
// If a coupon was already applied there's no need to apply it again
if ( $cart->has_discount( $coupon_code ) ) {
return;
}
$cart->apply_coupon( $coupon_code );
}
Now, I’m going to add a product to my cart to check if the coupon will be automatically applied.
Yup, I can see the coupon applied. This solution definitely works as expected.
This method will be a good choice for you if you want to control the discount’s usage rules more easily. Especially if you use WooCommerce plugins which extend the default WooCommerce coupons functionality.
Also, keep in mind that the code above won’t allow your users to remove the coupon from their carts. The only way to disable it is by using another individual coupon. Although, your users can still combine this coupon with other non-individual coupons.
How to restrict the manual usage of a coupon?
Since we want to give the discount to our regular customers only, we have to restrict the manual usage of this coupon code by other customers. To do this, we’ll need to use the woocommerce_coupon_is_valid hook.
add_filter( 'woocommerce_coupon_is_valid', 'process_autocoupon_usage_validation', 10, 3);
function process_autocoupon_usage_validation( $is_valid, $coupon, $that ) {
$coupon_code = $coupon->get_code();
if ( $coupon_code !== 'autocoupon' ) {
return $is_valid;
}
if ( ! is_user_logged_in() ) {
return false;
}
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$total_spent = floatval( $customer->get_total_spent() );
if ( $total_spent < 1000 ) {
return false;
}
return $is_valid;
}
Now, if a user tries to use this coupon being unauthorized or if their total spent is less than $1000, they will see an error:
This error message is not too comprehensive. Sometimes, your users just don’t notice that they forgot to log in. Let’s customize this error message by making it more informative. This time, we’ll use another WooCommerce built-in hook: woocommerce_coupon_error.
add_filter('woocommerce_coupon_error', 'custom_autocoupon_error_message', 10, 3);
function custom_autocoupon_error_message( $err, $err_code, $coupon ) {
$coupon_code = $coupon->get_code();
if ( $coupon_code !== 'autocoupon' ) {
return $err;
}
if ( ! is_user_logged_in() ) {
return 'You can not use this coupon being unauthorized.';
}
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$total_spent = floatval( $customer->get_total_spent() );
if ( $total_spent < 1000 ) {
return 'We are sorry, but you need to spend at least $1000 on our website to use this coupon.';
}
return $err;
}
This time, if I try to use the same coupon again, I’ll see the new error message:
How to create URL coupons in WooCommerce?
If you utilize email/messenger marketing in your business, you’ll probably find this method interesting. As you might have guessed, we’re going to create a coupon that will be automatically applied if a user comes to our website from a special URL.
How does this work?
For example, you can create a mass email campaign targeted at your regular customers. In the email, you can tell your clients how much you appreciate their loyalty and that you want to show your gratitude by giving them a discount for their next order. All they need to do to get the discount is to visit your website by the link that may look like this:
https://mystore.com/?coupon=autocoupon
On our end, we’re going to check if a user came to the website using a coupon URL. If that’s true, we’ll automatically apply a coupon code from the coupon URL parameter.
As you remember from the introduction, we want to automatically apply a coupon only if a user has spent on our website at least $1000. But you can get rid of this requirement on your website if you want to allow the use of this coupon by every website visitor.
I’m going to use the same coupon code I’ve created in the previous section.
Let’s see how to code a solution like this:
add_action( 'wp_loaded', 'auto_apply_coupon_from_url' );
function auto_apply_coupon_from_url() {
if ( is_admin() || wp_doing_ajax() ) {
return;
}
if ( ! isset( $_GET['coupon'] ) ) {
return;
}
if ( ! function_exists('WC') || ! WC()->session ) {
return;
}
$coupon_code = $_GET['coupon'];
$coupon = new WC_Coupon( $coupon_code );
// We have to check if a coupon does exist and it's valid
$discounts = new WC_Discounts();
$coupon_is_valid = $discounts->is_coupon_valid( $coupon );
// is_coupon_valid() function will return a boolean true if a coupon is valid
if ( $coupon_is_valid !== true ) {
return;
}
WC()->cart->apply_coupon( $coupon_code );
}
The code above will be executed on each frontend page. You can change this behavior by using WooCommerce conditional tags. The best part is that the is_coupon_valid() function will use the custom validation rules we’ve added in the previous section.
Now, if I open my website (being authorized) using a link like this: https://mystore/?coupon=AUTOCOUPON, the coupon code will be automatically applied to my cart.
How to auto-apply discounts in WooCommerce without coupons?
Both methods we’ve tried before forced us to create a coupon to auto-apply a discount. In this section, I’ll show you an additional way of how to auto-apply a discount in WooCommerce without any coupons required.
If you’d been working with WooCommerce for a while, you might have noticed that some plugins (usually it’s custom payment or shipping providers) add their own fees to your clients’ carts. They can look like this:
In most cases, those fees are added using the WC_Cart::add_fee() function. By the name of this method, you can guess that this function allows you to add your custom fees to the cart. This function takes three arguments: $name (name of your custom fee), $amount (its amount), $taxable (this argument should be set to true if a fee has to be taxable), and $tax_class (the tax class which has to be used for a fee if it’s taxable).
The most interesting part is that the add_fee() function can accept a negative number for a second argument. In this case, a custom fee will behave like a discount, reducing the total price of the cart.
Let’s see how to use this feature to implement the same logic we’ve created before:
add_action( 'woocommerce_cart_calculate_fees', 'auto_apply_discount_for_regular_users', 10, 1 );
function auto_apply_discount_for_regular_users( $cart ) {
// We should apply the discount for the authorized regular customers only
if ( ! is_user_logged_in() ) {
return;
}
$user_id = get_current_user_id();
$customer = new WC_Customer( $user_id );
$total_spent = floatval( $customer->get_total_spent() );
if ( $total_spent < 1000 ) {
return;
}
$subtotal = $cart->subtotal;
$discount_percentage = 5;
$discount = ( $discount_percentage / 100 ) * $subtotal;
// Keep your attention to the minus sign before the $discount variable
$cart->add_fee( 'Regular customer discount', -$discount );
}
Let’s check the cart page to see if the discount/fee works as expected.
As you see, I got a Regular customer discount of five percent of the cart subtotal amount. I really love this method because it allows to highlight the discount better, and there’s no need to use WooCommerce coupons.
Summary
As you see, if you want to auto-apply discounts in WooCommerce, there are many options to choose from. Coupons are great if you want to better control the logic behind the discount, and you’re okay with how they look on the front end. Additional fees will be a good choice if you want to code the discount logic on your own or/and you want to highlight the discount in the cart.
I hope you’ll find this article helpful. Don’t hesitate to experiment with the code examples from this post: this is the best way to learn more about WooCommerce and its features.
Feel free to ask any questions and share your opinion about this post in the comments section below. Thank you for your attention; see you in the next one!