2016-04-28 163 views
0

我不想限制某些產品頁面來自具有特定角色的註銷用戶或用戶。最簡單的方法可能是檢查產品頁面的類別ID,如果current_user_can('')比重定向到主商店頁面。限制產品類別爲'x'的產品頁面WooCommerce

但我真的不知道從哪裏開始..我應該添加一個動作init?我如何檢查當前頁面的產品ID?

我以爲我可以用var_dump()得到一些數據但是結果一無所獲。 我這樣做:

add_action('init', 'get_all_post_meta'); 

function get_all_post_meta() { 
    //$meta = get_post_meta(get_the_ID()); 
    global $post; 
    var_dump('$post'); 
    $metavar = get_the_terms($post->ID); 
    var_dump('$metavar'); 

} 

但我的控制檯沒有結果。

編輯:我發現我的var_dump()是不正確的,因爲它應該像var_dump($post);繼續我的追求吧。

回答

0

這是我迄今爲止的解決方案,現在我需要弄清楚如何刪除單個產品而不是全部。

add_action('wp_head', 'get_all_post_meta', 1); 

function get_all_post_meta() { 

    global $post; 
    $banned_cid = array(8); 
    $current_cid = array(); 
    $metavar = get_the_terms($post->ID, 'product_cat'); 
    global $woocommerce; 
    $redirect_url = 'http://www.example.nl/'; 


    if(current_user_can('subscriber') || current_user_can('manage_options')){}else if(is_product()){ 
     foreach ($metavar as $term) { 
         $cat_id .= $term->term_id.','; 
         array_push($current_cid, $term->term_id); 
        } 
     var_dump($current_cid); 

     $c = array_intersect($banned_cid, $current_cid); 
      if (count($c) > 0) {  
       $woocommerce->cart->empty_cart(); 
       wp_redirect($redirect_url, 302); 
       exit; 
     } 
    } 
} 
相關問題