为来宾用户或特定用户角色隐藏 WooCommerce 商店和存档页面上的特色产品
Hide featured products on WooCommerce shop and archives pages for guest users or for a certain user role
如果访问者未登录或如果用户角色是“客户”
,我试图限制产品出现在archives/search结果中]
我正在使用这个片段:
// 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 // <===
) );
测试时我确信我得到了未登录状态,因为特色产品不再显示,但我现在似乎可以无限加载。有什么建议吗?
您似乎使用了不必要的步骤。您的代码尝试的这种缩短和修改版本应该足以为未登录的客户或具有用户角色 'customer'
的用户隐藏特色产品
所以你得到:
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 );
如果访问者未登录或如果用户角色是“客户”
,我试图限制产品出现在archives/search结果中]我正在使用这个片段:
// 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 // <===
) );
测试时我确信我得到了未登录状态,因为特色产品不再显示,但我现在似乎可以无限加载。有什么建议吗?
您似乎使用了不必要的步骤。您的代码尝试的这种缩短和修改版本应该足以为未登录的客户或具有用户角色 'customer'
所以你得到:
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 );