If you want to restrict a payment method in WooCommerce based on products in the cart, this guide is for you. For example, you might want to disable Cash on Delivery (COD) whenever a product from a specific category is added to the cart.

Why Disable Payment Methods by Product Category?

Sometimes, stores need to restrict payment options based on the type of products being purchased. Common use cases:

  • High-value products – Disable COD or low-security methods.
  • Digital products – Force online payments only.
  • Category-specific promotions – Only allow certain gateways.

By implementing this snippet, you can ensure better control over payment options, reduce order risk, and improve checkout flexibility.

PHP Snippet: Disable Payment Method for a Specific Category

You can add this snippet to your child theme’s functions.php file or a custom plugin:

/**
 * Disable a specific payment method if a product from a certain category is in the cart
 * 
 * @hooked woocommerce_available_payment_gateways
 */
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_by_category' );

function disable_payment_gateway_by_category( $available_gateways ) {
    // Only apply on frontend checkout
    if ( is_admin() || ! is_checkout() ) return $available_gateways;

    $disable_gateway = false;
    $target_category_id = 8; // Replace with your category ID

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id'];
        $categories = get_the_terms( $product_id, 'product_cat' );

        if ( $categories && ! is_wp_error( $categories ) ) {
            foreach ( $categories as $category ) {
                if ( $category->term_id == $target_category_id ) {
                    $disable_gateway = true;
                    break 2; // Stop loop if category found
                }
            }
        }
    }

    // Disable COD if target category is in cart
    if ( $disable_gateway && isset( $available_gateways['cod'] ) ) {
        unset( $available_gateways['cod'] );
    }

    return $available_gateways;
}

How it works:

  • The code checks every product in the cart.
  • If a product belongs to the target category, the specified payment gateway (e.g., COD) is removed from the checkout.

Tips for Customization

  • Replace $target_category_id = 8; with the ID of your category.
  • Replace 'cod' with the payment gateway ID you want to disable. Examples:
    • PayPal: 'paypal'
    • Stripe: 'stripe'
  • You can also target multiple categories or gateways by extending the logic with arrays.

Alternative Options

If you prefer a plugin-based solution:

  1. Business Bloomer WooCommerce Toggle Payments By Category
    • Lightweight plugin with one feature: show/hide payment methods based on categories.
    • Lifetime license, no recurring fees.
    • Easy setup via an admin interface.
  2. Advanced WooCommerce Conditional Payment Gateways
    • Offers advanced rules for products, categories, user roles, and more.
    • Ideal for complex e-commerce stores.

Conclusion

Disabling a payment gateway for specific product categories in WooCommerce is simple and improves control over checkout options. Whether you use the PHP snippet or a lightweight plugin, you can enhance the customer experience and prevent payment issues with restricted products.