Filter available WooCommerce payment gateways by role [closed]
IT Nursery
May 14, 2022
0
I know WooCommerce is not well-received here, because it’s not directly related to WordPress, but as support on the WooCommerce forums is just that terrible and people here having proven they’re capable and knowledgeable, I’ll give it a try.
A client of ours wants to show/hide specific payment gateways for specific user groups. Here’s the situation:
User Group A: Customer
User Group B: Company
Only B is supposed to be able to pay via invoice, so after they receive the goods.
“cheque” is the name of the payment gateway used for invoice payment.
It’s not working yet, so what am I missing?
Any help is greatly appreciated!
1 Answer 1
Arrrrg a WooCommerce question… quick burn him at the stake!
There’s a couple of problems with your callback function above:
you are not declaring global $woocommerce so you do not have access to the $woocommerce global variable.
and $woocommerce->customer holds the state of the WC_Customer class however this class does not contain a get_role() method, further more you are not defining anywhere in your callback the value of the $role variable so that wouldn’t work anyway even if the method existed.
Instead, check for the user role in the roles array returned on the WP_User object:
Note: if the user is logged out, they will have no role, so you may wish to account for that and do something different such as redirect the user to login first before shopping depending on your use case, you can use is_user_logged_in() for that.