2017-08-16 102 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

相关问题