Disable Payment Gateways Methods for a Specific User Role
<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_function' );
function cxc_disable_cod_payment_gateway_function( $available_gateways ) {
//Disable Payment Gateways Methods for a Specific User Role customer
if( current_user_can( 'customer' ) ) {
if ( isset( $available_gateways[ 'cod' ] ) ) {
unset( $available_gateways[ 'cod' ] );
}
// if you need to disable multiple payment gateways just add similar code
// if ( isset( $available_gateways[ 'cxc_payment_gateway_2' ] ) ) {
// unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
// }
}
return $available_gateways;
}
?>
Enable Payment Gateways Methods for a Specific User Role
<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_function' );
function cxc_disable_cod_payment_gateway_function( $available_gateways ) {
//Enable Payment Gateways Methods for a Specific User Role customer
if( ! current_user_can( 'customer' ) ) {
if ( isset( $available_gateways[ 'cod' ] ) ) {
unset( $available_gateways[ 'cod' ] );
}
// if you need to disable multiple payment gateways just add similar code
// if ( isset( $available_gateways[ 'cxc_payment_gateway_2' ] ) ) {
// unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
// }
}
return $available_gateways;
}
?>
Enable Payment Gateway Methods for User are Registered
<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_user_not_logged_in', 999 );
function cxc_disable_cod_payment_gateway_user_not_logged_in( $available_gateways ) {
if( ! is_user_logged_in() ) {
if ( isset( $available_gateways[ 'cod' ] ) ) {
unset( $available_gateways[ 'cod' ] );
}
// if you need to disable multiple payment gateways just add similar code
// if ( isset( $available_gateways[ 'cxc_payment_gateway_2' ] ) ) {
// unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
// }
}
return $available_gateways;
}
?>
Was this article helpful?
YesNo