Prevent Coupon Code Sharing for WooCommerce – 2024

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.
See also  Better Woocommerce Out of Stock Message

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.

See also  WordPress Incremental Backup Plugin - The Best For WooCommerce

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.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×