在WooCommerce商店和档案页面上,隐藏了特色产品,供客人用户或某些用户角色

发布于 2025-01-30 02:50:54 字数 1613 浏览 2 评论 0原文

如果访问者未记录,我正在尝试限制产品出现在档案/搜索结果中,如果有“客户”角色是“客户”,

我正在使用此片段:

// Check User Role

function banks_has_user_role($check_role){
    $user = wp_get_current_user();
    if(in_array( $check_role, (array) $user->roles )){
        return true;
    }
    return false;
}

// // Hide products in specific category from not logged-in users and user role customer
add_filter( 'woocommerce_product_query_tax_query', 'exclude_products_fom_unlogged_users', 10, 2 );
function exclude_products_fom_unlogged_users( $tax_query, $query ) {
    global $user, $product;
    if( ! is_user_logged_in() ){
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
    }
    
    else if(banks_has_user_role('customer')){
        $tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
    }
    return $tax_query;
}


// The query
$query = new WP_Query( array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'tax_query'           => $tax_query // <===
) );

测试时,我确定我有未登录状态以不再显示特色产品,但我现在似乎已获得无限的负载。有建议吗?

I am attempting to restrict products from appearing in archives/search results if a visitor is NOT LOGGED IN OR if there user Role is "Customer"

I'm using this snippet:

// Check User Role

function banks_has_user_role($check_role){
    $user = wp_get_current_user();
    if(in_array( $check_role, (array) $user->roles )){
        return true;
    }
    return false;
}

// // Hide products in specific category from not logged-in users and user role customer
add_filter( 'woocommerce_product_query_tax_query', 'exclude_products_fom_unlogged_users', 10, 2 );
function exclude_products_fom_unlogged_users( $tax_query, $query ) {
    global $user, $product;
    if( ! is_user_logged_in() ){
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
    }
    
    else if(banks_has_user_role('customer')){
        $tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
    }
    return $tax_query;
}


// The query
$query = new WP_Query( array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'tax_query'           => $tax_query // <===
) );

When testing I was certain I got the not logged in state to work as featured products no longer displayed but I seem to get infinite loading now. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2025-02-06 02:50:54

看来您正在使用不必要的步骤。您的代码尝试的缩短和修改版本应该足以让未登录或使用用户角色'CODER>'CONDUSTER''的用户隐藏特色产品,

因此您可以得到:

function filter_woocommerce_product_query_tax_query( $tax_query, $query ) {
    // NOT for backend
    if ( is_admin() ) return $tax_query;

    // Guest user OR user role = customer
    if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'NOT IN',
        );
    }

    return $tax_query;
}
add_filter( 'woocommerce_product_query_tax_query', 'filter_woocommerce_product_query_tax_query', 10, 2 );

It seems you are using unnecessary steps. This shortened and modified version of your code attempt should suffice to hide featured products for customers who are not logged in or for users with the user role 'customer'

So you get:

function filter_woocommerce_product_query_tax_query( $tax_query, $query ) {
    // NOT for backend
    if ( is_admin() ) return $tax_query;

    // Guest user OR user role = customer
    if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'NOT IN',
        );
    }

    return $tax_query;
}
add_filter( 'woocommerce_product_query_tax_query', 'filter_woocommerce_product_query_tax_query', 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文