有条件地隐藏一些 Woocommerce 产品类别和 wpml

Hide conditionally some Woocommerce product categories and wpml

我试图只显示 Wordpress 中特定用户角色的类别。 我发现这段代码有效,因为它在未登录或我具有不同的用户角色时不显示类别产品。

但我遇到的问题如下: 该网站使用 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' );

所以英语类别 ID 是 127,荷兰语类别 ID 是 128。 有人可以帮我让它重新工作吗?

希望有人能帮助我吗?


更新

英语和荷兰语现在仅在用户角色为批发商时显示类别。但我的网站上有更多语言。

这是具有相应类别 ID 的完整列表:

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

如何让它支持 2 种以上的语言?

更新2(完成WPML检测语言代码/产品类别ID)

  1. 我已将 'field' => 'id' 替换为 'field' => 'term_id'(参见相关文档 linked):
  2. 我使用 WPML 常量 ICL_LANGUAGE_CODE 来检测语言并设置正确的类别。因此,例如,如果您使用 "english" 版本的网站,产品类别 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 产品类别 ID。


相关文档:WP_Query Taxonomy Parameters