提问者:小点点

为用户角色隐藏支付网关


嘿,伙计们,我的wordpress/Woocomece网站上有一种货到付款方式,我想对客户用户角色和未登录用户隐藏这种方式。

我一直在上下搜索,唯一能找到的就是这段代码。

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' );

有人能帮我修改这个代码,让它为我的使用工作。提前谢谢你!


共2个答案

匿名用户

已经提到了为您尝试和测试的代码。它工作得很好。让我知道你是否也一样。

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);

匿名用户

<?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;
}
?>

//--试试这个,我已经使用了这个代码来反对最低订单限制