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
Cash on deliverycod

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

Leave a Reply

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