Discounts are a great way to increase sales and helps to build a good customer relationship. It is a way to attract your customers to come back to your store and purchase it again.
WooCommerce also provides a feature to store owners for adding coupons for their customers. And they use different ways to reach out to their customers for the coupons like sending them emails, adding a banner on their website, and so on. But sometimes it is possible that the customers may forget to apply the coupon during the checkout.
So if as a store owner you do not want your customers to purchase products without using coupons, you have to find a way that the coupons are applied automatically to the cart when customers purchase. It can be done by providing coupon code URLs to customers via emails or add them on the website, which when clicked, the coupon will be applied automatically to the cart. Or the simpler way to achieve this is to apply the coupon code automatically when a customer adds a product to the cart.
In WooCommerce right now there is no option to apply the coupons automatically but it is possible to achieve this by adding a code snippet or using some of the extensions available.
WooCommerce Auto Apply Coupon Using a Code Snippet
Let us take an example here, where the store owner wants to give a discount of 20% for the orders valued more than $250 on the occasion of Halloween or Black Friday or Christmas and he wants that none of his customers should miss the discount. So to achieve this, there are 2 ways which we will be discussing below.
1. Auto apply coupon code directly to the cart when the product is added
For this, the first step is to create a coupon code in WooCommerce with a Minimum Spend Usage restriction to $250 under the Add New Coupon / Edit Coupon -> Usage Restrictions menu.
Recommended Reading: How to Hide the WooCommerce Coupon code field based on specific products |
The coupon is added as per the owner’s requirements but as entering the coupon is still a manual process here, it is highly possible that the customer may forget to use the coupon code. So the best way to avoid this is to apply the code automatically to the cart. This can be achieved by adding the below code to the functions.php file of the child theme or using any code snippets plugin.
/** * Apply Coupon automatically to the cart */ function ts_apply_discount_to_cart() { $order_total = WC()->cart->get_subtotal(); if( $order_total > 250 ) { $coupon_code = '20%OFF'; if ( !WC()->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) { WC()->show_messages(); } } } add_action( 'woocommerce_before_cart_table', 'ts_apply_discount_to_cart' );
Here we have used woocommerce_before_cart_table hook which is in the WooCommerce cart form before displaying the cart table. In this hook, we are checking for the order total and if the order total is $250 or more, then we are applying the ‘20%OFF’ coupon to the cart using add_discount() function.
2. WooCommerce Auto Apply Coupon Using Code URLs
Another way of applying coupon code to the cart directly is by providing the Coupon Code URLs to the customers by sending across emails, newsletters, or by posting them on the website in a banner. So when a customer clicks on the URL and if the product is already added to the cart, the coupon code will be applied automatically.
Related Article: How to Hide ‘Shipping Via’ Text from Thank You and View Order Pages in WooCommerce?
To achieve this, you need to add the below code in the functions.php file of your child theme.
/** * Set session variable on page load if the query string has coupon_code variable. */ function ts_get_custom_coupon_code_to_session() { if( isset( $_GET[ 'coupon_code' ] ) ) { // Ensure that customer session is started if( !WC()->session->has_session() ) WC()->session->set_customer_session_cookie(true); // Check and register coupon code in a custom session variable $coupon_code = WC()->session->get( 'coupon_code' ); if( empty( $coupon_code ) && isset( $_GET[ 'coupon_code' ] ) ) { $coupon_code = esc_attr( $_GET[ 'coupon_code' ] ); WC()->session->set( 'coupon_code', $coupon_code ); // Set the coupon code in session } } } add_action( 'wp_loaded', 'ts_get_custom_coupon_code_to_session' ); /** * Apply Coupon code to the cart if the session has coupon_code variable. */ function ts_apply_discount_to_cart() { // Set coupon code $coupon_code = WC()->session->get( 'coupon_code' ); if ( ! empty( $coupon_code ) && ! WC()->cart->has_discount( $coupon_code ) ){ WC()->cart->add_discount( $coupon_code ); // apply the coupon discount WC()->session->__unset( 'coupon_code' ); // remove coupon code from session } } add_action( 'woocommerce_add_to_cart', 'ts_apply_discount_to_cart', 10, 0 );
Here we have used two hooks wp_loaded and woocommerce_add_to_cart. Where in wp_loaded hook, we are checking if the current URL contains coupon_code query var or not and if it contains then the value of the variable is stored in the WooCommerce session. In woocommerce_add_to_cart hook, we are checking that if the coupon_code variable is set in the WooCommerce session, then apply that coupon code to the cart.
After adding the above code you can use the below URL to send it to your customers.
https://mywebsite.com/?coupon_code=20OFFChristmas
Or if you want to redirect the URL directly to the cart or checkout page after applying coupon, you can use the below URLs.
https://mywebsite.com/cart/?coupon_code=20OFFChristmas
https://mywebsite.com/checkout/?coupon_code=20OFFChristmas
Once the coupon is applied to the cart, you don’t want your customers to add another coupon to the same cart. So it is better to hide the coupon code field on the cart and checkout page. Here is another post that explains How to Hide the WooCommerce Coupon code field.
Providing customizable products to your customers?
Product Input Fields Pro for WooCommerce plugin offer 19 different field types for your product page so that you can get clear product customization info from your customers.
Each field has its own unique feature, values, restrictions, and customization options to give you the flexibility in getting clear information from the users. You can choose where to place the custom fields on the page and how to style them, and you can also set additional charges for your custom input field values.
WooCommerce Auto Apply Coupon Using WooCommerce Extensions
Another way to achieve this is by using different WooCommerce extensions which allows you to apply the coupon code automatically to the cart. Below are some of the extensions:
- Smart Coupons – This plugin has everything you need to grow sales and customers using discounts, coupons, credits, vouchers, product giveaways, offers, and promotions. It’s the best-selling and most complete coupon management plugin for WooCommerce.
- WooCommerce URL Coupons – The WooCommerce URL Coupons extension helps you create unique URLs that automatically apply a discount and products to the customer’s shopping cart. You can change where the customer is redirected after visiting the unique URL, and your URL coupons will still obey all coupon restrictions, such as usage restrictions.
- Advanced Coupons – Advanced Coupons extends your coupon features so you can market your store better.
- WooCommerce Extended Coupon Features FREE – “WooCommerce Extended Coupon Features” adds functionality to the WooCommerce coupons and allows for automatic discount rules. Very easy to use, the functionality is conveniently integrated to the WooCommerce Edit Coupon panel.
- Auto Coupons for WooCommerce – It is a simple & lightweight plugin just allowing coupons to be applied automatically to the cart if the set rules meet.
So you can either use the code snippets or the plugins to offer the customer an unmissed discount for the purchases.
Hi, I have an issue when I click add to cart button on my WordPress woo commerce store it auto discounts 30%
Hi Muhibullah, Are you using the code mentioned in this blog post? Are you facing any issues with it?
Thank you for this article. This worked but created another issue. If you try to visit the cart page after adding another item to the cart after the coupon is already applied, the cart page hangs after posting the error “Coupon Code Already Applied” and will not show the cart contents.
Hi Jacob, Thank you for bringing this issue to our attention. I was able to reproduce the issue after I followed the steps you mentioned. I have passed it on to our Development team. I will post the revised code here as well as modify it in the blog post once I hear back from them. Please give it a couple of days.
This worked for me on a client’s site. Thanks for posting this. I did have problems with the site’s caching though. Sometimes, the coupon code didn’t automatically show up.
Hi Tom, I am glad it worked for you. Caching can sometimes give wrong results or issues if it is not setup correctly.
Thank you so much for the code snippet. I was surprised to find apply by URL wasn’t a core feature of Woo Commerce and was disappointed to think I’d need to buy another plug in.
I agree, Susan. This should be a default feature of WooCommerce.
The code snippet only works for logged in users.
Hi Dinah, The code snippet does work for logged-in users as well. I just tested both code snippets.