Learn in this tutorial how to hide a payment method on WooCommerce checkout page for a specific product category with a simple code snippet.
WooCommerce allows you to offer different payment methods, you can even arrange them to be displayed to the user as you want (in WooCommerce > Settings > Payments).
The limitation is that all payment methods will be displayed in all products and you may want to hide some method depending on the type of product.
The good thing is that you can use the WooCommerce woocommerce_available_payment_gateways
filter next to a product category to hide the payment method you are not interested in displaying.
Here’s how to do it:
Snippet to hide a payment method from WooCommerce checkout for a specific product category
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1091 code snippets of this website, login or subscribe here.
In this case you use the woocommerce_available_payment_gateways
filter together with a conditional where you indicate that when the product category is “books” the payment by wire transfer option is not displayed.
You can customize the code to hide the payment method you want. You will only have to modify unset( $available_gateways['bacs'] )
by another one. For example, if you want to hide Paypal you would use unset( $available_gateways['paypal'] ).
And if you wanted to hide Stripe it would be unset( $available_gateways['stripe'] ).
Similarly, you can customize the product category where you want the code to be applied. To do this, replace “books” with the category you want, you can even add more categories by separating them with a comma.
Conclusion
As you can see, with a few lines of code you can hide a payment method on the WooCommerce checkout page only for a specific product category.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉