Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide certain WooCommerce gateways from non-admin users
<?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
You can’t perform that action at this time.