In WooCommerce, applying coupons automatically can enhance the shopping experience by providing immediate rewards to customers as they meet certain spending thresholds. This tutorial will demonstrate how to implement “Automatic Coupons Based on Cart Total” using a custom function in your WordPress theme. By the end of this guide, you’ll be able to add a special discount automatically when the customer’s cart total reaches a specified amount.
Prerequisites
Before proceeding, ensure you have administrative access to your WordPress site and are comfortable editing PHP files. It’s recommended to perform these changes in a child theme to avoid losing customizations when updating the parent theme.
Step 1: Define the Coupon Details
First, you need to create a coupon in WooCommerce:
- Go to WooCommerce > Coupons from your WordPress dashboard.
- Click Add Coupon.
- Define a coupon code, for example,
20OFF
. - Set the discount type to a fixed cart discount or a percentage discount as desired.
- Configure any additional restrictions or limits on the coupon usage.
- Save the coupon.
Step 2: Add Custom Code to Your Theme’s functions.php
File
Next, add the following PHP code to your theme’s functions.php
file. This code checks the cart total and applies the “Automatic Coupons Based on Cart Total” when the total exceeds a specified amount.
/*
* Snippet: Automatic Coupons Based on Cart Total – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?959
* Tested with WooCommerce 8.8.2
*/
add_action('woocommerce_before_cart', 'wcsuccess_apply_automatic_coupon');
add_action('woocommerce_before_checkout_form', 'wcsuccess_apply_automatic_coupon');
function wcsuccess_apply_automatic_coupon() {
$minimum_amount = 100; // Set minimum cart total required to apply the coupon
$coupon_code = '20OFF'; // The coupon code you want to apply
if ( WC()->cart->subtotal >= $minimum_amount ) {
if ( ! WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->add_discount( $coupon_code );
wc_print_notices();
}
} else {
if ( WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->remove_coupon( $coupon_code );
wc_add_notice('The automatic discount has been removed as your total has dropped below ' . wc_price($minimum_amount), 'notice');
}
}
}
Explanation of the Code
- Hooks: The
wcsuccess_apply_automatic_coupon
function is hooked intowoocommerce_before_cart
andwoocommerce_before_checkout_form
to check the cart’s total each time the cart or checkout page is loaded. - Cart Total Check: It checks if the cart subtotal (before taxes and shipping) is at least
$minimum_amount
(you can adjust this value as needed). - Apply Coupon: If the cart meets the minimum amount and the coupon hasn’t been applied yet, it applies the coupon.
- Remove Coupon: If the cart total falls below the minimum required after the coupon has been applied, it removes the coupon and informs the user.
Testing Your Changes
- Clear your WooCommerce cache and browser cache.
- Add products to your cart and observe how the “Automatic Coupons Based on Cart Total” is applied or removed based on the total amount.
- Adjust the
$minimum_amount
and$coupon_code
as necessary to fit your specific campaign.
Conclusion
This tutorial provides a straightforward method to enhance customer engagement by applying “Automatic Coupons Based on Cart Total” in WooCommerce. It automatically rewards customers when they reach a certain spending threshold, fostering increased order values and customer satisfaction.
By integrating this functionality, you can offer a seamless discount experience, encouraging more purchases and boosting overall sales. Always test changes on a staging site before applying them to your live site to ensure compatibility with your specific WooCommerce setup.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch