# Hide a Payment Method for Certain Products

> Removes a payment method, for example cash on delivery or buy-now-pay-later, when the cart contains products from categories it should not be used for.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, payments, categories, checkout
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-hide-gateway-for-category/

## Code

```php
$wpide_gateway_cat_rules = array( // Gateway ID => category slugs that hide it.
    'cod' => array( 'gift-cards', 'digital' ),
);

add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) use ( $wpide_gateway_cat_rules ) {
    if ( ( is_admin() && ! wp_doing_ajax() ) || ! WC()->cart ) {
        return $gateways;
    }

    foreach ( WC()->cart->get_cart() as $item ) {
        foreach ( $wpide_gateway_cat_rules as $gateway => $categories ) {
            if ( isset( $gateways[ $gateway ] ) && has_term( $categories, 'product_cat', $item['product_id'] ) ) {
                unset( $gateways[ $gateway ] );
            }
        }
    }

    return $gateways;
} );
```

## Notes

The payment method comes back as soon as those products leave the cart.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hide a Payment Method for Certain Products" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
