woocommerce_available_payment_gateways

The woocommerce_available_payment_gateways filter receives the payment methods WooCommerce considers available in the current context. Its callback must return an array keyed by internal gateway IDs, with WC_Payment_Gateway objects as values. IDs such as cod (Cash on Delivery) and bacs (bank transfer) are not the translated titles customers see at checkout.

TypeFilter hook
Argument and return value$gateways (array)
SourceWooCommerce payment gateways class

Hide Cash on Delivery for carts without shipping

add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) {
    if ( WC()->cart && ! WC()->cart->needs_shipping() ) {
        unset( $gateways['cod'] );
    }

    return $gateways;
} );

This removes Cash on Delivery when the cart does not need shipping, such as a cart containing only virtual products. The cart check avoids assuming a cart always exists when gateways are queried. This filter removes an available choice; it does not register a new payment method. Put the callback in a site-specific plugin or child theme's functions.php and test it with relevant carts and payment options.

Checkout behavior and compatibility

Removing every available gateway can prevent a customer from paying. Check mixed physical and virtual carts, shipping destinations, and orders that do not need payment. Gateway availability may also be queried outside a normal customer checkout; code that relies on the current cart or session should guard against missing context. For conditions based on shipping zones rather than whether shipping is needed, see the shipping-zone payment methods guide.

This filter is used by classic checkout and has partial support in the Checkout block. Removing an existing gateway can work there, but adding or changing a payment method may require a block integration. See WooCommerce's block hook compatibility notes and verify the rendered checkout.