2016-12-06 112 views
6

在我的虛擬商店中使用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請點這個?

感謝

回答

3

正確的代碼做你想做的(用戶角色蛞蝓是在較低的情況下,我使用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文件中使用。

相關問題