Last active
February 21, 2019 17:22
-
-
Save mtruitt/c89dd17ee29a259c95ccc2c0e3657270 to your computer and use it in GitHub Desktop.
Woocommerce - Disable/allow payment gateways based on user role.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Filter to remove/add payment gateways at checkout | |
| add_filter( 'woocommerce_available_payment_gateways', 'mt_disable_gateways' ); | |
| function mt_disable_gateways( $available_gateways ) { | |
| // Get current user | |
| $user = wp_get_current_user(); | |
| // Array to handle roles you want to allow this for. | |
| $allowed_roles = array( 'administrator'); | |
| // Array_intersect compares the values of arrays to see if any match. | |
| if( array_intersect( $user->roles, $allowed_roles ) ) { | |
| // return all active | |
| return $available_gateways; | |
| } else { | |
| // Remove the one we are testing from everyone else | |
| unset( $available_gateways['paypal_pro_payflow'] ); | |
| // Return modified available gateways | |
| return $available_gateways; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment