How to Disable Payment Method for Specific Category

WooCommerce Disable Payment Method for Specific CategoryAre you running a WooCommerce store and you want to know how you can disable payment methods for specific categories or multiple payment methods based on the product categories that are present in the cart?

If you are looking for a solution that is focused on product categories, but with some small changes that could also be changed for shipping classes for example on the products themselves, this article will guide you on how to achieve that.

Disable Payment Method for Specific Category

A good example of where this approach is useful is when the WooCommerce store owners have a category containing only high price products and want customers to pay with bank transfers instead of using a credit card. This is just an example of the many situations in which you may need to disable a payment method for a category.

If you are not tech-savvy you might opt for a premium plugin to do the job for you. An example is the WooCommerce Conditional Payment Plugin, which helps you create conditions in which your payment methods will be enabled or disabled. If you are not that great at using code, this will be the best premium solution that you should consider.

Moreover, this plugin allows you to create unlimited conditions and use, for example, cart totals, billing country, user role, and much more to define which payment gateway shows and which not.

WooCommerce Conditional Payment Plugin

However, for this brief tutorial, I am going to show you how to disable a payment method for a specific category using PHP code snippets.

Steps to Disable Payment Method for Specific Category Using PHP Code Snippet

Here are the simple steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will disable the payment method for a specific category.
  3. Add the following code to the PHP file:
/**
 * @snippet       Disable Payment Method for Specific Category
*/


add_filter( 'woocommerce_available_payment_gateways', 'njengah_unset_gateway_by_category' );


function njengah_unset_gateway_by_category( $available_gateways ) {

    if ( is_admin() ) return $available_gateways;

    if ( ! is_checkout() ) return $available_gateways;

    $unset = false;

    $category_ids = array( 8, 37 );

    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {

        $terms = get_the_terms( $values['product_id'], 'product_cat' );   

        foreach ( $terms as $term ) {       

            if ( in_array( $term->term_id, $category_ids ) ) {

                $unset = true;

                break;

            }

        }

    }

    if ( $unset == true ) unset( $available_gateways['cheque'] );

    return $available_gateways;

}
  1. These will be the resultsdisable a payment method for a specific category

Remember to add the correct category ID that is in your WooCommerce store for this code to work.

If you do not like using the Category ID, then you can also use the category slug in your WooCommerce store. You may opt to use them, as they are easier to understand for future reference.

Steps to Disable Payment Method for Specific Category Using Category Slugs

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will disable the payment method for a specific category.
  3. Add the following code to the PHP file:
/**

 * @snippet Disable payment gateway based on category.

*/

function njengah_disable_payment_gateway_category( $gateways ) {

// Categories that'll disable the payment gateway

$category_slugs = array( ' accessories', 'another-category' );

$category_ids = get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $category_slugs, 'fields' => 'ids' ) );


// Check each cart item for given category

foreach ( WC()->cart->get_cart() as $item ) {

$product = $item['data'];

if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {

unset( $gateways['cod'] ); // Disable payment gateway 'cod' when product has one of the categories

break;

}

}

return $gateways;

}

add_filter( 'woocommerce_available_payment_gateways', 'njengah_disable_payment_gateway_category' );
  1. This will be the resultdisable payment method for specific category

Remember to add the correct category slug and you need to define the payment gateway that you want to disable. For my example, I have used ‘cod’ to represent cash on delivery.

It is important to note that the payment gateway identifier also needs to be changed to whichever you would like to disable when the given category is present in the cart.

  • Bank transfer (bacs)
  • Cheque (cheque)
  • Cash on delivery (cod)
  • PayPal (PayPal)

However, if you are not using one of the default payment gateways, do not worry, as there is still an easy way to find out the payment gateway ID. You can do this by going to the payment gateway settings page and you will find the ID in there.

For example for the Cash on Delivery option in the settings page, this will be the URL: wp-admin/admin.php?page=wc-settings&tab=checkout&section=cod

Conclusion

In this post, you have learned how to disable the Payment Method for a Specific Category using two methods. You can define them using product category IDs or category slugs for easy future reference. Remember to use the category ID or slug that is in your WooCommerce store to add this functionality.

Similar Articles