Woocommerce hide payment gateway for user roles

2019-04-15 23:37发布

Hey guys I have a cash on delivery payment method on my wordpress/woocomerce website that I want to hide from the customer user role and non-logged in users.

I've been searching up and down and the only thing I found close was this bit of code.

function paypal_disable_manager( $available_gateways ) 

{global $woocommerce;

if ( isset( $available_gateways['paypal'] ) && current_user_can('customer') ) {

unset( $available_gateways['paypal'] );

}

return $available_gateways;

}

add_filter( 'woocommerce_available_payment_gateways','paypal_disable_manager' );

Would someone be able to help me modify this code to make it work for my use. Thank you in advance!

2条回答
迷人小祖宗
2楼-- · 2019-04-15 23:58
<?php
//--- Filter for remove any payment gateway as per the user role selected --
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
    global $woocommerce, $current_user;

    if ( is_user_logged_in() ) {        
        $userRole = implode(',',$current_user->roles);
        if($userRole == 'my_user_role'){
            //-- Remove casho on delivery if following user have logged in
            unset($gateways['cod']);        
        }   
    }else{
        //-- Hide COD if user not logged in 
        unset($gateways['cod']);
    }           
 return $gateways;
}
?>

//-- Try this one, I have already make use of this code against minimum order limit

查看更多
The star\"
3楼-- · 2019-04-16 00:09

Have mention the code which is tried and tested for you. It works well. Lemme know if the same works for you too.

function wdm_disable_cod( $available_gateways ) {

    //check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
    if ( isset($available_gateways['cod']) && (current_user_can('customer') || ! is_user_logged_in()) ) {

        //remove the cash on delivery payment gateway from the available gateways.

         unset($available_gateways['cod']);
     }
     return $available_gateways;
}

add_filter('woocommerce_available_payment_gateways', 'wdm_disable_cod', 99, 1);
查看更多
登录 后发表回答