如何根据用户角色隐藏具有给定类别的 WooCommerce 产品 [英] How do I hide WooCommerce products with a given category based on user role

查看:33
本文介绍了如何根据用户角色隐藏具有给定类别的 WooCommerce 产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a client with the following issue:

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

I need to be able to split their WooCommerce WordPress site into essentially two categories. If a user is logged in as a "Wholeseller", only products in the "Wholesale" category get pulled from the database.

但是,如果用户未登录或已登录但不是批发商",则只有没有批发"的产品 category 从数据库中获取.

However if the user is not logged in or is logged in but not a "Wholeseller", then only products without the "Wholesale" category get pulled from the database.

我想我会在主题的 functions.php 文件中添加这样的内容:

I figure I'll add something like this to the theme's functions.php file:

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.
    }
}

我已经浏览了 StackOverflow,但是我没有找到我要找的东西,或者我完全不知道我应该使用哪些关键字来进行搜索.

I've browsed around StackOverflow, but I haven't found what I'm looking for or quite know what keywords I should be using for my searches.

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

Can you point me in the right direction?

推荐答案

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

Yes it is possible. There is 2 ways:

1) 使用 pre_get_posts wordpress 钩子,该钩子在创建查询变量对象之后但在实际查询运行之前调用.所以它非常适合这种情况.我们在这里假设'批发'类别的ID是'123'.

1) With the pre_get_posts wordpress hook that is called after the query variable object is created, but before the actual query is run. So it is perfect for this case. We imagine here that the ID of 'Wholesale' category is '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 挂钩.(我们在这里仍然假设'批发'类别的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 文件中,或者更好地位于自定义插件中.

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

If you want to use the category slug instead of the category ID you will have to replace partially (both arrays) with:

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

<小时>

如果您愿意和需要,您可以添加一些woocommerce条件标签if 语句 进一步限制这一点.


You could add if you wish and need, some woocommerce conditionals tags in the if statements to restrict this even more.

参考资料:

这篇关于如何根据用户角色隐藏具有给定类别的 WooCommerce 产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆