Learn in this tutorial how to choose the default payment method on the checkout page in WooCommerce for a specific product category with a little bit of code.
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 this will be the order for all products and you may want to prioritize a certain method depending on the type of product.
The good thing is that you can use the WooCommerce woocommerce_available_payment_gateways
filter along with a product category to get this more granular control.
Here’s how to do it:
Snippet to choose the default WooCommerce checkout payment method in a particular product category
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 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 default option to make the payment is Stripe.
You can customize the code so that the default method is the one you want. You will only have to modify $default_gateway = 'stripe'
for another one. For example, if you want Paypal to be the default method you would use $default_gateway = 'paypal'
. And for bank transfers you would use $default_gateway = 'bacs'
.
Similarly, you can customize the product category where you want the code to be applied. To do this, replace ‘books’ with the category of your choice.
Conclusion
As you can see, with a few lines of code you can customize the default payment method on the WooCommerce checkout page only for a specific product category.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂