Add Rate Limiting to WooCommerce Checkout, Registration, and Coupon Endpoints

Rate limiting for the WooCommerce checkout, registration and coupons

Card testing bots use small shops to check stolen card numbers: they place hundreds of cheap orders, each with a different card, and the shop pays the gateway fees and risks its merchant account. Other bots register thousands of accounts or try coupon codes until one works. A limit per IP address and time window stops most of this, in a few dozen lines.

Problem: The payment gateway reports a wave of declined payments from the same few IP addresses, the user list fills with fake customers, and coupon codes are guessed.

Solution: Write a small counter helper with a fixed window per action and IP, check it in woocommerce_checkout_process (failed checkouts), woocommerce_registration_errors (registration) and woocommerce_coupon_is_valid (coupon attempts), and add a wc_add_notice() error or a WP_Error when the limit is reached.

The same pattern for the login form: rate limiting the WordPress login.

The counter

function ha_rate_limited( $action, $limit, $window ) {
    $ip  = WC_Geolocation::get_ip_address();
    $key = 'ha_rl_' . md5( $action . '|' . $ip );

    $entry = get_transient( $key );
    if ( ! is_array( $entry ) || $entry['until'] < time() ) {
        $entry = [ 'count' => 0, 'until' => time() + $window ];
    }
    $entry['count']++;
    set_transient( $key, $entry, $entry['until'] - time() );

    return $entry['count'] > $limit;
}

The end of the window is stored in the value, so every new attempt does not push the expiry further. Without a persistent object cache, transients are rows in wp_options; that is fine for these volumes. With Redis, they live in memory.

WC_Geolocation::get_ip_address() reads proxy headers such as X-Forwarded-For first. Behind Cloudflare or a load balancer that is correct; on a server that is reached directly, a bot can send any value in that header. In that case use $_SERVER['REMOTE_ADDR'].

Checkout

add_action( 'woocommerce_checkout_process', function () {
    if ( ha_rate_limited( 'checkout', 5, 15 * MINUTE_IN_SECONDS ) ) {
        wc_add_notice( __( 'Too many order attempts. Please try again in a few minutes.', 'textdomain' ), 'error' );
    }
} );

Five attempts in fifteen minutes is plenty for a real customer whose card was declined once or twice. The hook runs for the classic checkout, after the form is submitted and before the order is created and the payment is attempted.

Registration

add_filter( 'woocommerce_registration_errors', function ( $errors ) {
    if ( ha_rate_limited( 'register', 3, HOUR_IN_SECONDS ) ) {
        $errors->add( 'ha_rate_limit', __( 'Too many registrations from your network. Please try later.', 'textdomain' ) );
    }
    return $errors;
} );

Coupons

add_filter( 'woocommerce_coupon_is_valid', function ( $valid, $coupon ) {
    if ( ha_rate_limited( 'coupon', 10, 10 * MINUTE_IN_SECONDS ) ) {
        throw new Exception( __( 'Too many coupon attempts. Please wait a few minutes.', 'textdomain' ) );
    }
    return $valid;
}, 10, 2 );

WooCommerce runs this filter inside its coupon validation and turns the exception into the error message shown to the customer. The counter counts every check, also the re-validation when the cart changes, so keep the limit generous.

NOTE: Rate limits slow bots down; they do not stop a botnet with thousands of addresses. Enable the fraud tools of the gateway (Stripe Radar, for example) and consider a CAPTCHA on the checkout as well. The Checkout block sends orders through the Store API, not through woocommerce_checkout_process; recent WooCommerce versions have their own rate limiting option for the Store API, which should be enabled for block-based checkouts.