如何根据用户角色隐藏具有给定类别的 WooCommerce 产品

How do I hide WooCommerce products with a given category based on user role

我有一个客户有以下问题:

我需要能够将他们的 WooCommerce WordPress 网站基本上分为两类。如果用户以 "Wholeseller" 身份登录,则只会从数据库中提取 "Wholesale" 类别 中的产品。

但是,如果用户没有登录或登录但没有 "Wholeseller",则只有没有 [=12= 的产品] 类别 从数据库中提取。

我想我会在主题的 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.
    }
}

我浏览了 Whosebug,但我没有找到我要找的东西,也没有完全知道我应该使用什么关键字进行搜索。

你能给我指出正确的方向吗?

是的,这是可能的。有两种方式:

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 文件中,或者在自定义插件中更好。

如果您想使用类别 slug 而不是类别 ID,则必须将部分(两个数组)替换为:

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

如果您愿意和需要,可以在 if statements 中添加 some woocommerce conditionals tags 以进一步限制这一点。

参考文献:

为了提高性能并仅在 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。

function exclude_categories_for_vendors( $query ) {

    // Get the current user
    $current_user = wp_get_current_user();
     $ARRAY_OF_PRODUCT_CATEGORIES_IDS_YOU_WANT_HIDDEN = array(26,23,20);

    if( is_user_logged_in() ){

        if ( $query->is_main_query() ) {
           
            if ( in_array( 'editor', (array) $current_user->roles ) ) {
                
                //echo "editor";
                //below query will hide categories for Vendor user in Admin Panel
                
                   $query->set( 'tax_query', array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        'terms' => $ARRAY_OF_PRODUCT_CATEGORIES_IDS_YOU_WANT_HIDDEN, // your category ID
                        'operator' => 'NOT IN'
                    )
                ) ); 

            } else{
                //no changes
            }
        }
    }
}
add_action( 'pre_get_posts', 'exclude_categories_for_vendors' );