移除添加到购物车按钮WooCommerce中的特定用户角色

问题描述:

在我的虚拟商店中使用Divi主题和woocommerce我有两组用户:最终用户和我的经销商,在我的最终客户的情况下只需要出现“购买”按钮。已经为我的经销商提供了“添加到订单”按钮(由YITH Request A Quote插件提供)。 万一无疑将是对如何删除添加到购物车按钮为经销商账户,我知道使用的代码:移除添加到购物车按钮WooCommerce中的特定用户角色

remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); 
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

我从整个网站删除的按钮,但我想用一些种类if只能定义一个组。 事情是这样的:

$user = wp_get_current_user(); 
if (in_array('Revenda', (array) $user->roles)) { 
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); 
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 
} 

或本:

if(current_user_can('revenda')) { 
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); 
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 
} 

我也尝试这种代码:

function get_user_role() { 
    global $current_user; 

    $user_roles = $current_user->roles; 
    $user_role = array_shift($user_roles); 

    return $user_role; 
} 

function user_filter_addtocart_for_shop_page(){ 
    $user_role = get_user_role(); 
    $role_id = get_role('Revenda'); 
    if($user_role == $role_id){ 
     remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10); 
    } 
} 

凡get_user_role将被显示我怎么能a请点这个?

感谢

正确的代码做你想做的(用户角色蛞蝓是在较低的情况下,我使用get_userdata(get_current_user_id())来获取用户数据。

所以我已经改变了一点点什么您的代码:

function remove_add_to_cart_for_user_role(){ 
    // Set Here the user role slug 
    $targeted_user_role = 'revenda'; // The slug in "lowercase" 
    $user_data = get_userdata(get_current_user_id()); 
    if (in_array($targeted_user_role, $user_data->roles)) { 
     remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10); 
     remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 
    } 
} 
add_action('init', 'remove_add_to_cart_for_user_role'); 

我已经嵌入是在init

0打响了一个函数的代码。

此代码已经过测试并且功能完善。

代码发送到您活动的子主题(或主题)的function.php文件中。或者也可以在任何插件php文件中使用。