Skip to content

Instantly share code, notes, and snippets.

@mircian
Last active May 29, 2018 11:13
Show Gist options
  • Save mircian/b17b2c808206bc4eb9e17caee9814e39 to your computer and use it in GitHub Desktop.
Save mircian/b17b2c808206bc4eb9e17caee9814e39 to your computer and use it in GitHub Desktop.
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