In this (short) post I want to show how you can disable one or more payment gateways based on the product categories that are present in the cart. This is focussed on product categories, but with some small changes could also be changed for shipping classes for example or products themselves.

Disable Payment Gateways by Product Category

With the following code snippet you can disable the ‘cod’ (Cash on Delivery) payment gateway when there’s a product from the category ‘pine-tree’ present in the cart. The thought process here is that this product can only be paid online and not on delivery.

In this snippet the product category slug is used and will need to be changed to the category slug that you want to disable the payment gateway for. The category ID can also be used, but personally I like to use slugs as that is easier to understand for future reference.

The payment gateway identifier also needs to be changed to whichever you’d like to disable when the given category is present in the cart.

Finding the Payment Gateway ID

WooCommerce has a number of payment gateways available by default. These have the following IDs;

Bank transferbacs
Chequecheque
Cash on deliverycod
PayPalpaypal

If you’re not using one of the default payment gateways, don’t worry, there’s still a easy way to find out the payment gateway ID. The easiest way to do this is by going to the payment gateway settings page and you’ll find the ID in there. For example, this is the settings page for the Cash on Delivery option; wp-admin/admin.php?page=wc-settings&tab=checkout&section=cod.

About the author: Jeroen Sormani is actively building WordPress, WooCommerce and Easy Digital Downloads plugins. Slightly obsessed by writing high quality code.
Follow Jeroen on Twitter

5 comments

  1. Hi, This code snippet works brilliantly, thank you very much for posting it. The only thing I have noticed is that if any products are variable products rather than simple products, then the payment gateway is not removed for that category.

    I tried changing the variable product to a simple product and it worked. Is there something I can add to the snippet to enable it to work for variable products too if they are in the categories to which we want to remove the payment gateway?

    Thank you.

    1. Hi Rob,

      Thank you for noticing; I’ve just made a adjustment to the snippet to account for variable products.

      Cheers,
      Jeroen

    1. Hi Dave,

      Could you post the specific error you’re getting? That would help narrow down what may be causing this.

      Cheers,
      Jeroen

Leave a Reply

Your email address will not be published. Required fields are marked *