Managing failed payments is a common challenge for WooCommerce store owners, especially when dealing with carding attacks or abandoned payment attempts. Without proper limits, customers or bots can repeatedly attempt payments, creating hundreds of failed order emails and cluttering your WooCommerce database.

In this guide, we’ll show you how to automatically cancel WooCommerce orders after three failed payment attempts, keeping your store clean, secure, and user-friendly.

Why Limit Payment Attempts in WooCommerce?

WooCommerce allows unlimited retries on the Order Pay page. While this is convenient for legitimate customers, it can also:

  • Cause unnecessary email notifications
  • Create database clutter with failed orders
  • Increase the risk of fraudulent payment attempts

By tracking failed payments and auto-cancelling orders after a set number of retries, store owners can prevent these issues and improve overall store management.

How to Auto-Cancel Orders After 3 Failed Payments

You can achieve this with a small PHP snippet. The snippet below works with WooCommerce Stripe Payment Gateway, but it can be adapted for other payment gateways that provide a hook on failed payments.

Key Features of This Snippet

  • Tracks failed payment attempts for each order
  • Automatically cancels orders after the third failed attempt
  • Adds an order note explaining the cancellation reason
  • Helps prevent bots from abusing the payment system

SEO-Friendly PHP Snippet

/**
 * @snippet       WooCommerce Auto-Cancel Orders After 3 Failed Stripe Payments
 * @description   Tracks failed Stripe payments and cancels order after 3 attempts
 * @author        Your Name
 * @compatible    WooCommerce 9+
 */

add_action( 'wc_gateway_stripe_process_payment_error', 'auto_failed_payments', 10, 2 );

function auto_failed_payments( $error, $order ) {

    if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
        return;
    }

    $order_id = $order->get_id();

    // Get previous failed attempts
    $attempts = intval( $order->get_meta( '_stripe_failed_attempts', true ) );
    $attempts++;

    // Save updated attempt count
    $order->update_meta_data( '_stripe_failed_attempts', $attempts );
    $order->save_meta_data();

    // Cancel order after 3 failed attempts
    if ( $attempts >= 3 ) {
        $order->update_status( 'cancelled', sprintf(
            'Order automatically cancelled after %d failed Stripe payment attempts',
            $attempts
        ));
    }
}

How It Works

  1. Every time a Stripe payment fails, the snippet runs automatically.
  2. It tracks the number of failed attempts in the order meta (_stripe_failed_attempts).
  3. If the payment fails three times, the order status changes to Cancelled.
  4. An order note is added, explaining the reason for cancellation.

This ensures both security and convenience, preventing bots from abusing the checkout system while keeping your store’s order table clean.

Benefits of Auto-Cancelling Failed Orders

  • Reduces database clutter from failed orders
  • Minimizes email spam during payment attacks
  • Improves store security by limiting repeated payment attempts
  • Enhances store management and makes it easier to identify genuine failed payments

Conclusion

Limiting payment retries and auto-cancelling orders after repeated failures is an essential WooCommerce best practice. Whether you’re dealing with bots, carding attacks, or simply abandoned transactions, this simple PHP snippet can save you time, reduce errors, and protect your store.