【问题标题】:Hide conditionally some Woocommerce product categories and wpml有条件地隐藏一些 Woocommerce 产品类别和 wpml
【发布时间】:2017-08-16 13:03:05
【问题描述】:

我正在尝试仅针对 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,荷兰语是 128。 有人可以帮我恢复这个工作吗?

希望有人能帮我解决这个问题吗?


更新

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

这里是完整的列表以及相应的类别 ID:

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

如何使它适用于 2 种以上的语言?

【问题讨论】:

    标签: php wordpress woocommerce categories wpml


    【解决方案1】:

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

    1. 我已将'field' => 'id' 替换为'field' => 'term_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 产品类别 ID。


    相关文档:WP_Query Taxonomy Parameters

    【讨论】:

    • 感谢您帮助我!我已将代码添加到我的 functions.php 中,但它不起作用。我一直在使用荷兰语的商店页面上看到隐藏类别的产品。我尝试添加一些其他类别 ID,但它仍然无法正常工作。有任何想法吗? @LoicTheAztec
    • 嗨@LoiTheAztec 非常感谢!现在唯一的问题是,当用户角色不是批发商时,它只会显示 id 为 127 的类别。但它需要反过来吗?因此,当用户角色是批发商时,它只需要显示类别 127、128。
    • 非常感谢!有用!但现在我正在尝试对其他语言(如德语、西班牙语等)做同样的事情。所以我尝试添加这些类别 ID。但结果是一个空白的网站,否则它将不再工作。因此,除了荷兰语和英语之外,我还有其他语言,并试图将它们隐藏在这些语言中。
    • 对不起!我刚刚做了 :) 希望我们能完成这个 :) 再次感谢!
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2022-08-24
    • 2018-07-18
    相关资源
    最近更新 更多