在 WooCommerce 中对未经授权的用户完全隐藏产品 [英] Completely hide products from unauthorized users in WooCommerce
问题描述
如果用户不是特定角色(例如经过验证的买家),我正在尝试从未登录的用户中完全删除一个或多个产品.
I'm trying to remove a product/products completely from Users that are not logged in and if user is not specific role (e.g Verified Buyer).
我已经能够使用下面的代码创建一个名为 Verified Buyer 的新角色;
I have been able to create a new role called Verified Buyer using the code below;
add_role(
'verified_buyer',
__( 'Verified Buyer', 'text-domain' ),
array(
'read' => true,
'edit_posts' => false,
)
);
//This Role is same role capability as the WooCommerce Customer role
我还使用下面的代码向 WooCommerce 添加新产品页面添加了一个复选框
and i have as well added a checkbox to the WooCommerce Add New Product page using the code below
function hide_product_from_unathorised_users() {
$args = array(
'id' => '_hide_from_unauthorize_users',
'label' => 'Hide Product from unauthorized users',
'desc_tip' => true,
'description' => __('Check this to hide this product from unauthorized users', 'text-domain')
);
woocommerce_wp_checkbox( $args );
}
add_action( 'woocommerce_product_options_advanced', 'hide_product_from_unathorised_users' );
// Save Fields
function product_custom_fields_save($post_id){
// Custom Product Text Field
$hide_product_unathorised_users = isset( $_POST['_hide_from_unauthorize_users'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_hide_from_unauthorize_users', esc_attr( $hide_product_unathorised_users ));
}
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
现在我有这两个选项(用户角色和一个复选框来知道要隐藏哪个产品)...如果满足以下条件,我想隐藏此类产品;
完全隐藏产品(甚至从搜索查询中)如果;
1. 勾选产品&用户未登录
2. 勾选产品&用户登录 &未经验证的买家或管理员角色
Now that i have this two options (user role and a checkbox to know which product to hide) ... I want to Hide such product if the following conditions are met;
HIDE PRODUCT COMPLETELY (Even from search Queries) IF;
1. Checkbox is checked on product & User is not logged in
2. Checkbox is checked on product & User logged in & not Verified Buyer or admin Role
像这样
function hide_product_completely_conditionally() {
global $post;
$hide_product_checkbox = get_post_meta( $post->ID, '_hide_from_unauthorize_users', true )
$user = wp_get_current_user();
$authorized_user_role = in_array( 'verified_buyer', (array) $user->roles );
$admin_role = in_array( 'administrator', (array) $user->roles );
if ( ($hide_product_checkbox == 'yes' && !is_user_loggedin()) || ($hide_product_checkbox == 'yes' && is_user_loggedin() && (!$authorized_user_role || !$admin_role) ) ) {
//(...) HIDE SUCH PRODUCT COMPLETELY CODE THAT I'M NOT SURE HOW TO WRITE
}
}
提前感谢您的帮助.
推荐答案
当用户不被允许时,以下代码将根据您的自定义产品字段过滤产品(如果他们尝试将他们重定向到商店页面手动访问受保护的产品).
The following will code will filter products based on a your custom product field when users are not allowed (and will redirect them to shop page if they try to access manually to a protected product).
// Conditional function checking for authorized users
function is_authorized_user(){
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] )
|| in_array( 'verified_buyer', $user->roles ) ) return true;
}
else return false;
}
// Filter product query (and search) from unauthorized users
add_filter( 'woocommerce_product_query_meta_query', 'only_authorized_users_meta_query', 10, 2 );
function only_authorized_users_meta_query( $meta_query, $query ) {
// Hide specific products from unauthorized users
if( ! is_authorized_user() && ! is_admin() ) {
$meta_query['relation'] = 'OR';
$meta_query[] = array(
'key' => '_hide_from_unauthorize_users',
'value' => 'no',
'compare' => '='
);
$meta_query[] = array(
'key' => '_hide_from_unauthorize_users',
'compare' => 'NOT EXISTS'
);
}
return $meta_query;
}
// Security: Redirect unauthorized users if accessing prodtected products
add_action( 'template_redirect', 'only_authorized_users_redirect' );
function only_authorized_users_redirect() {
// Specific products redirect for unauthorized users (to be sure)
if( is_product() && ! is_authorized_user()
&& get_post_meta( get_the_id(), '_hide_from_unauthorize_users', true ) === 'yes' ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
您可以:
1) 使用 WC_Customer
is_paying_customer
布尔属性,例如:
1) Use use the WC_Customer
is_paying_customer
boolean property like:
if( WC()->customer->get_is_paying_customer() ) {
// Is a playing customer
} else {
// NOT a playing customer
}
2) 或添加自定义用户元为:
2) Or adding custom user meta as:
update_user_meta( get_current_user_id(), 'allowed_customer', '1' );
然后您将使用以下方法进行检查:
Then you will check using:
if( get_user_meta( get_current_user_id(), 'allowed_customer', true ) ) {
// Allowed customer
} else {
// NOT Allowed customer
}
这篇关于在 WooCommerce 中对未经授权的用户完全隐藏产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!