How to Intercept WooCommerce Coupon Apply Request for Rate Limiting Attempts by using a Hook?

I’m working on a WooCommerce site where I’ve implemented a rate limit for applying coupon codes. I created a separate database table to track user IPs and coupon usage through a function has_exceeded_coupon_attempts($email), which checks if a user has exceeded their allowed coupon attempts. Works fine, tested with

add_action('woocommerce_applied_coupon', 'track_coupon_attempts_jg8766576yGdf', 10, 1); 

Currently, I need a way / hook to execute this tracking function inside my functions.php file before the discount request is sent, ideally preventing the ajax form submission which request url looks like

...com/?wc-ajax=apply_coupon

from submitting if the rate limit is exceeded.

Are there specific hooks or filters that I should utilize for this purpose? I appreciate any guidance or code examples that could help me achieve this. Thank you!

1. Server-Side: Hook into the woocommerce_apply_coupon filter to prevent the coupon application:

This hook is triggered when a coupon is being applied via an AJAX call. You can check the rate limit and return an error if the limit is exceeded.

// Hook into WooCommerce before the coupon is applied
add_filter( ‘woocommerce_apply_coupon’, ‘check_coupon_rate_limit_before_applying’, 10, 1 );

function check_coupon_rate_limit_before_applying( $coupon_code ) {
// Get the current user’s email or IP address
$current_user_email = WC()->customer->get_billing_email();

// Replace with your function to check coupon usage
if ( has_exceeded_coupon_attempts( $current_user_email ) ) {
    // Return an error message, this will stop the coupon from being applied
    wc_add_notice( 'You have exceeded the allowed number of coupon usage attempts.', 'error' );
    return false; // Prevent coupon application
}

return $coupon_code; // Proceed with applying the coupon

}

This function checks the rate limit using your has_exceeded_coupon_attempts() function and prevents the coupon from being applied if the limit is exceeded. The wc_add_notice() function is used to display an error message to the user.

2. Client-Side: Prevent the form submission via JavaScript

You can also use JavaScript to prevent the AJAX request from being sent at all. Here’s how you can intercept the WooCommerce apply_coupon AJAX request and add custom validation:

jQuery(document).ready(function($) {
$(‘#coupon_code’).on(‘input’, function() {
var couponCode = $(this).val();
var exceededAttempts = false;

    // Use AJAX to check if the coupon rate limit is exceeded
    $.ajax({
        url: wc_checkout_params.ajax_url,
        type: 'POST',
        data: {
            action: 'check_coupon_attempts',
            coupon_code: couponCode
        },
        async: false, // Make it synchronous to prevent form submission
        success: function(response) {
            if (response.exceeded) {
                exceededAttempts = true;
                alert('You have exceeded the allowed number of coupon attempts.');
            }
        }
    });

    // Prevent form submission if rate limit is exceeded
    if (exceededAttempts) {
        return false;
    }
});

});

3. Server-Side: Handle the AJAX request for the rate limit check

On the server side, you’ll need to handle the AJAX request that checks if the user has exceeded the coupon usage limit:

add_action( ‘wp_ajax_check_coupon_attempts’, ‘check_coupon_attempts_ajax’ );
add_action( ‘wp_ajax_nopriv_check_coupon_attempts’, ‘check_coupon_attempts_ajax’ );

function check_coupon_attempts_ajax() {
// Get the current user’s email or IP address
$current_user_email = WC()->customer->get_billing_email();

// Replace with your function to check coupon usage
$exceeded = has_exceeded_coupon_attempts( $current_user_email );

// Return the result in JSON format
wp_send_json( array( 'exceeded' => $exceeded ) );

}