Coupon codes are a great tool for driving sales and promoting customer loyalty. However, sharing of exclusive or one-time-use coupons can lead to unintended losses. This tutorial will show you how to prevent coupon code sharing for WooCommerce by ensuring that certain coupons are only usable by customers who have not used them before and are logged into their accounts.
Prerequisites
Before you start, ensure you have administrative access to your WordPress site and are comfortable editing PHP files. Implementing these changes in a child theme is recommended to avoid losing them when updating the main theme.
Step 1: Add Custom Code to Your Theme’s functions.php
File
Open your theme’s functions.php
file and add the following PHP code:
/*
* Snippet: Prevent Coupon Code Sharing for WooCommerce – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?977
* Tested with WooCommerce 8.8.3
*/
// Prevent Coupon Code Sharing for WooCommerce
add_filter('woocommerce_coupon_is_valid', 'wcsuccess_validate_coupon_usage', 10, 2);
function wcsuccess_validate_coupon_usage($is_valid, $coupon) {
// Check if the coupon has already been used by the user
if (!is_user_logged_in()) {
// If user is not logged in, invalidate the coupon
throw new Exception(__('You must be logged in to use this coupon.', 'woocommerce'));
}
// Get the current user ID
$user_id = get_current_user_id();
// Check if this user has already used this coupon
global $wpdb;
$usage_count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta ON order_items.order_item_id = itemmeta.order_item_id
LEFT JOIN {$wpdb->prefix}posts as posts ON order_items.order_id = posts.ID
WHERE posts.post_status IN ('wc-completed', 'wc-processing')
AND itemmeta.meta_key = '_used_coupons'
AND itemmeta.meta_value = %s
AND posts.post_author = %d",
$coupon->get_code(), $user_id
));
if ($usage_count > 0) {
// If the coupon has been used by this user, invalidate it
throw new Exception(__('You have already used this coupon.', 'woocommerce'));
}
return $is_valid;
}
Explanation of the Code
- Check Login Status: The code first checks if the user is logged in. If not, it invalidates the coupon immediately.
- Query Past Orders: It then checks the user’s past completed and processing orders to see if the coupon has already been used by them.
- Prevent Reuse: If the user has used the coupon before, an exception is thrown, and the coupon is invalidated for that user.
Step 2: Test Your Coupon Restrictions
After adding the code:
- Log into your WooCommerce site as a customer.
- Try applying the coupon to your cart.
- Complete a purchase with the coupon and then attempt to use it again on a new order.
- Ensure that the coupon cannot be applied a second time by the same user.
Customising Further
You can extend this functionality by customising which order statuses are checked (e.g., including refunded orders) or by creating a user interface for managing these coupon rules directly in the WordPress admin.
Conclusion
Implementing measures to prevent coupon code sharing for WooCommerce can help maintain the exclusivity of your promotions and protect your marketing strategy. By ensuring that each coupon can only be used once per customer, you reduce the risk of widespread sharing and maintain the intended incentive effect of your coupons.
This tutorial on how to prevent coupon code sharing for WooCommerce is just one way to enhance the control you have over your eCommerce promotions. Testing changes on a staging site before applying them to your live site is always recommended to ensure compatibility and functionality.
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