Skip to content

Instantly share code, notes, and snippets.

@mtruitt
Last active February 21, 2019 17:22
Show Gist options
  • Select an option

  • Save mtruitt/c89dd17ee29a259c95ccc2c0e3657270 to your computer and use it in GitHub Desktop.

Select an option

Save mtruitt/c89dd17ee29a259c95ccc2c0e3657270 to your computer and use it in GitHub Desktop.
Woocommerce - Disable/allow payment gateways based on user role.
<?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