2017-08-16 101 views
1

我正試圖在Wordpress中爲特定的用戶角色顯示一個類別。 我發現這一點的代碼是可行的,因爲它沒有顯示類別產品時沒有登錄或當我有不同的用戶角色。有條件地隱藏一些Woocommerce產品類別和wpml

但我遇到的問題如下: 該網站正在使用WPML,我的代碼僅適用於英文。但不適用於其他語言。所以我加入了測試另一個類別id,它是相同的類別,但只有這一個是針對荷蘭語的,所以我期待它適用於英語和荷蘭語,但對於英語而言它不會工作。

我現在使用的代碼是:

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' => 'id', 
      'terms' => '127,128', // 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' => 'id', 
      'terms' => '127,128', // your category ID 
      'operator' => 'NOT IN' 
     ) 
    )); 
} 
} 
add_action('woocommerce_product_query', 'wholeseller_role_cat'); 

所以英語類別編號爲127和荷蘭語它的128 有人可以幫助我得到這個工作了?

希望任何人都可以幫助我嗎?


更新

的英國和荷蘭的語言,現在只顯示該類別時,用戶角色是Wholeseller。但我的網站上有更多的語言。

下面是完整的列表與相應的類別ID:

English (en) => 117 
Dutch (nl) ===> 118 
French (fr) ==> 131 
Italian (it) => 134 
Spanish (es) => 137 
German (de) ==> 442 

我怎樣才能使它超過2種語言的工作?

回答

1

更新2

  1. 我已經'field' => 'term_id'取代'field' => 'id'(完全WPML檢測語言碼/產品類別ID)(見相關文檔linked):
  2. 我使用WPML恆定ICL_LANGUAGE_CODE到檢測語言並設置正確的類別。因此,舉例來說,如果你在你的網站上的「英語」版本的產品類別ID將127和荷蘭語這將是128 ...的語言代碼和類別ID陣列中的設置(在代碼的開頭)。
  3. 我壓縮了代碼。

這裏是更新並壓實代碼:

add_action('woocommerce_product_query', 'wpml_product_category_and_user_role_condionnal_filter', 10, 1); 
function wpml_product_category_and_user_role_condionnal_filter($q) { 

    // Set HERE this indexed array for each key (as language code in 2 letters)… 
    // …and the corresponding category ID value (numerical) 
    $lang_cat_id = array('en' => 127, 'nl' => 128, 'fr' => 131, 'it' => 134, 'es' => 137, 'de' => 442,); 

    // With WPML constant "ICL_LANGUAGE_CODE" to detect the language and set the right category 
    foreach($lang_cat_id as $lang => $cat_id) 
     if(ICL_LANGUAGE_CODE == $lang) $category_id = $cat_id; 

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

    // Displaying only "Wholesale" product category to "whole seller" user role (IN) 
    // or displaying other product categories to all other user roles (NOT IN) 
    $operator = in_array('wholeseller', $current_user->roles) ? 'IN' : 'NOT IN' ; 

    // The conditional query 
    $q->set('tax_query', array(array(
     'taxonomy' => 'product_cat', 
     'field' => 'item_id', // Replaced 'id' by 'term_id' (see documentation) 
     'terms' => $category_id, // Id depending on language 
     'operator' => $operator // Depending on user roles 
    ))); 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

這已經過測試,適用於多語言WPML產品類別標識。


相關文檔:WP_Query Taxonomy Parameters

+0

謝謝你幫我看看這個!我已將代碼添加到我的functions.php中,但它不起作用。我一直使用荷蘭語在商店頁面上看到隱藏類別的產品。我嘗試添加一些其他類別的ID,但它仍然不起作用。有任何想法嗎? @LoicTheAztec – 123MijnWebsite

+0

非常感謝你! – 123MijnWebsite

+0

嗨@LoiTheAztec非常感謝你!現在唯一的一點是,當用戶角色不是暢銷商時,它將只顯示ID爲127的類別,但它需要反過來呢?所以當用戶角色是暢銷商時,它只需顯示類別127,128。 – 123MijnWebsite

相關問題