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

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

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

但是,如果用户登录登录但不是“Wholeseller”,则只有没有“Wholesale”类别的产品才会从数据库删除.

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

我浏览过StackOverflow,但是我还没找到我正在寻找的内容,或者非常清楚我应该使用哪些关键字进行搜索.

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

解决方法:

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

1)使用在创建查询变量对象之后但在运行实际查询之前调用的pre_get_posts wordpress钩子.所以它非常适合这种情况.我们在这里想象’批发’类别的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钩子. (我们仍然在这里想象’批发’类别的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语句中使用some woocommerce conditionals tags来限制更多.

参考文献:

> Modifying the WooCommerce Product Query
> I can’t fetch WooCommerce products by category id
> WordPress Class Reference – WP Query

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...