2016-12-06 3 views
6

woocommerce와 함께 Divi 테마를 사용하는 가상 저장소에서 최종 사용자의 경우 최종 사용자의 경우 두 사용자 그룹 즉, 최종 리셀러 만 필요합니다. '구매'버튼이 표시됩니다. 이미 리셀러에게만 "주문에 추가"버튼 (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은 표시됩니다 어떻게해야합니까? 이걸 훔쳐?

감사

답변

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 파일에서.

관련 문제