Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins

How to Disable Payment Method for Specific Category @ WooCommerce Checkout?

As an online store owner, there are a lot of reasons involved in disabling a payment method. But mostly a store owner would restrict WooCommerce payment methods for certain products due to their high value or delivery requirements. For example, if product categories such as heavy electronic items require special handling, then you may disable Cash On Delivery to ensure that customers have paid for the order before it is shipped. It not only avoids payment-related complications but allows for better control over the payment options presented to customers, ensuring that only suitable methods are available for each category.

This post helps you to disable a payment method based on product category during the checkout process.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary Steps

  • Decide the payment method you are going to disable.
  • Find and replace the category ID with your actual category ID in the code.

Follow the steps given below to find the category ID of a product:

  1. Log in to your WordPress admin dashboard.
  2. Go to Products > categories
  3. Look for a product category, hover over it, and click Edit
  4. The category ID is in the URL at the top of your browser, typically as “tag_ID=XX.”
How to Disable Payment Method for Specific Category @ WooCommerce Checkout?

Solution: Disable Payment Method for Specific Category @ WooCommerce Checkout

Suppose you have an online store that has many product categories, including electronic items, clothing, and more. If you want to restrict the use of a specific payment method for certain product categories, the below code snippet will accomplish that.

add_filter( 'woocommerce_available_payment_gateways', 'ts_unset_gateway_by_category' );
function ts_unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_id = 58; // TARGET CATEGORY
    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( $term->term_id == $category_id ) {
                $unset = true; // CATEGORY IS IN THE CART
    if ( $unset == true ) unset( $available_gateways['cod'] ); // DISABLE COD IF CATEGORY IS IN THE CART
    return $available_gateways;


The below output shows that the Cash on Delivery payment option has been disabled since the cart has items of the category ID specified in the code.

How to Disable Payment Method for Specific Category @ WooCommerce Checkout?

Code Explanation

  • The code starts by adding a filter to the woocommerce_available_payment_gateways hook.
  • It associates the filter with the custom function ts_unset_gateway_by_category, which will be executed when the filter hook is invoked.
  • The custom function ts_unset_gateway_by_category is initialized to handle the filter’s execution. It takes the available payment gateways as an argument.
  • A context check is performed to determine whether the current environment is within the WooCommerce admin area using if ( is_admin() ). If the check returns true, the code immediately returns the original list of available payment gateways. 
  • Another context check verifies whether the current context corresponds to the checkout page with if ( ! is_checkout() ). If this condition is met, the code also returns the unaltered list of available payment gateways.
  • The code initializes a Boolean variable named $unset, setting its initial value to ‘false’. This variable will serve as a marker to indicate the presence or absence of a specific product category in the customer’s shopping cart.
  • A designated product category is identified by its unique ID, which in this instance is set to 58. This category ID is pivotal in triggering the deactivation of the ‘Cash on Delivery’ (COD) payment gateway.
  • The code proceeds to examine the contents of the customer’s shopping cart by iterating through the items using WC()->cart->get_cart_contents(). For each item, the code checks whether the product belongs to the specified product category. 
  • If any product in the cart belongs to the specified category, the $unset variable is set to ‘true’, indicating that the category is present in the cart.
  • In the event that the $unset variable is ‘true,’ signifying the presence of the specified category in the cart, the code employs the unset() function. This action effectively removes the ‘cod’ payment gateway from the list of available payment gateways, disabling it for the duration of the checkout process.
  • The code concludes by returning the modified list of available payment gateways. 


The above code snippets will disable a certain payment method based on the category ID of the cart item. Alternatively, you can also enable a WooCommerce payment method based on order total that allows you to optimize payment processing costs.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x