Hide certain WooCommerce gateways from non-admin users
This file contains 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 | |
/** | |
* @param $available_gateways array of available WooCommerce gateways | |
* | |
* @return array Updated gateways | |
*/ | |
function m_disable_gateways_for_loggedout_users( $available_gateways ) { | |
if ( ! is_user_logged_in() && ! current_user_can( 'manage_options' ) ) { | |
// array of the gateways ids you want to exclude | |
$gateways_to_exclude = array( | |
'stripe', | |
); | |
foreach ( $gateways_to_exclude as $gateway ) { | |
if ( isset( $available_gateways[ $gateway ] ) ) { | |
unset( $available_gateways[ $gateway ] ); | |
} | |
} | |
} | |
return $available_gateways; | |
} | |
// priority 0 to avoid errors | |
add_filter( 'woocommerce_available_payment_gateways', 'm_disable_gateways_for_loggedout_users', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment