2016-08-17 63 views
4

我有以下問題客戶端的特定類別:如何隱藏WooCommerce產品具有基於用戶角色

我需要能夠把自己的WooCommerce WordPress站點分成兩個基本類別。如果用戶以"Wholeseller"登錄,則僅從"Wholesale"類別中的產品被從數據庫中拉出。

但是,如果用戶沒有登錄或已經登錄,但不是"Wholeseller",則只有產品無的"Wholesale"類別獲得從數據庫中抽取。

我想我會喜歡這個添加了一些主題的functions.php文件:

add_filter("some_woocommerce_hook", "wholeseller_filter"); 

function wholeseller_filter() { 
    if (current_user->role == "Wholeseller"){ 
     //omit products without "wholesale" category while browsing whole site 
    } else { 
     //omit products with "wholesale" in the category while browsing whole site. 
    } 
} 

我周圍StackOverflow上瀏覽,但我還沒有找到我要找的還是很清楚什麼是關鍵詞我應該使用我的搜索。

您能否指點我正確的方向?

回答

5

是的,這是可能的。有兩種方法:

1)與pre_get_posts在創建查詢變量對象之後但在運行實際查詢之前調用的wordpress掛鉤。所以對於這種情況非常適合。我們在這裏想象一下,'Wholesale'類別的ID是'123'

下面是自定義代碼:

function wholeseller_role_cat($query) { 

    // Get the current user 
    $current_user = wp_get_current_user(); 

    if ($query->is_main_query()) { 
     // Displaying only "Wholesale" category products to "whole seller" user role 
     if (in_array('wholeseller', $current_user->roles)) { 
      // Set here the ID for Wholesale category 
      $query->set('cat', '123'); 

     // Displaying All products (except "Wholesale" category products) 
     // to all other users roles (except "wholeseller" user role) 
     // and to non logged user. 
     } else { 
      // Set here the ID for Wholesale category (with minus sign before) 
      $query->set('cat', '-123'); // negative number 
     } 
    } 
} 
add_action('pre_get_posts', 'wholeseller_role_cat'); 

這一代碼進入你的活動的子主題或主題,或自定義插件更好的function.php文件。


2)同woocommerce_product_query WooCommerce鉤。 (我們在這裏還是想象一下,'Wholesale'類別的ID是'123'

下面是自定義代碼:

function wholeseller_role_cat($q) { 

    // Get the current user 
    $current_user = wp_get_current_user(); 

    // Displaying only "Wholesale" category products to "whole seller" user role 
    if (in_array('wholeseller', $current_user->roles)) { 
     // Set here the ID for Wholesale category 
     $q->set('tax_query', array(
      array(
       'taxonomy' => 'product_cat', 
       'field' => 'term_id', 
       'terms' => '123', // your category ID 
      ) 
     )); 

    // Displaying All products (except "Wholesale" category products) 
    // to all other users roles (except "wholeseller" user role) 
    // and to non logged user. 
    } else { 
     // Set here the ID for Wholesale category 
     $q->set('tax_query', array(
      array(
       'taxonomy' => 'product_cat', 
       'field' => 'term_id', 
       'terms' => '123', // your category ID 
       'operator' => 'NOT IN' 
      ) 
     )); 
    } 
} 
add_action('woocommerce_product_query', 'wholeseller_role_cat'); 

這一代碼進入你的活動的子主題或主題,或自定義插件更好的function.php文件。

如果你要使用的類別塞代替類別ID,你將不得不與部分替代(都是數組):

  array(
       'taxonomy' => 'product_cat', 
       'field' => 'slug', 
       'terms' => 'wholesale', // your category slug (to use the slug see below) 

,如果你願意,你需要你可以添加,some woocommerce conditionals tagsif statements甚至更​​多地限制。

參考文獻:

+0

'pre_get_posts'所有的東西!雖然我會建議不要將它保留在主題的'functions.php'中,而應將其移入插件中。 – helgatheviking

+1

'functions.php'是最簡單的放置快速測試片段的地方,但我總是建議使用您的主題來顯示相關的功能,並在插件中保留任何功能特定的代碼。 – helgatheviking

+0

您不應該在pre_get_posts上運行此操作,除非您希望在每個查詢中運行此操作。 Woocommerce鉤子「woocommerce_product_query」將允許您僅在woocommerce查詢中運行它。 –

0

爲了使這個更好的性能和對woocommerce查詢使用下面的函數和掛鉤纔有效。

function exclude_product_from_wholesale($q){ 

$current_user = wp_get_current_user(); 
$ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array(); 

if (in_array('wholeseller', $current_user->roles)) { 
    $q->set('post__not_in', $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN); 
} 

} 

add_action('woocommerce_product_query', 'exclude_product_from_wholesale'); 

你可以放棄這個簡單的函數到你的functions.php。

相關問題