如何解决根据用户角色和自定义分类显示/隐藏产品
我正在尝试根据用户角色显示/隐藏产品。我有一个可以完美地用于索引/搜索/类别页面的功能,但是如果您在单个产品页面上,我将尝试隐藏该产品的代码。它基本上停留在网站任何页面的重定向循环中。
基本上,如果您以“ super_wholesale_user”身份登录,并且该产品不在分类术语ID“ 380”中,则该产品将重定向您并向您显示该通知。
我正在使用的代码是这样:
// On single product pages,redirect to shop and display a custom notice for specific products
add_action( 'template_redirect','wcpp_redirect_hiding_product_detail' );
function wcpp_redirect_hiding_product_detail() {
if ( ( is_user_logged_in() || ! current_user_can('super_wholesale_customer') )
&& has_term( '380','Role' ) && is_product() ) {
}
else {
// Add a notice (optional)
wc_add_notice(__("Sorry,but you are not allowed yet to see this product."),'notice' );
// Shop redirection url
$redirect_url = get_permalink( get_option('woocommerce_shop_page_id') );
wp_redirect($redirect_url); // Redirect to shop
exit(); // Always exit
}
}
解决方法
我知道了。以下代码供参考:
// On single product pages,redirect to shop and display a custom notice for specific products
add_action( 'template_redirect','wcpp_redirect_hiding_product_detail' );
function wcpp_redirect_hiding_product_detail() {
if ( is_product() ) {
if ( ( is_user_logged_in() || ! current_user_can('super_wholesale_customer') )
&& has_term( '380','Role' ) && is_product() ) {
}
else {
// Add a notice (optional)
wc_add_notice(__("Sorry,but you are not allowed yet to see this product."),'notice' );
// Shop redirection url
$redirect_url = get_permalink( get_option('woocommerce_shop_page_id') );
wp_redirect($redirect_url); // Redirect to shop
exit(); // Always exit
};
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。