我是一个插件,以隐藏特定的产品类别,只为用户的角色。
add_action( 'woocommerce_product_query', 'wcpp_hide_product' );
function wcpp_hide_product( $q ) {
//if main query, skip
if ( ! $q->is_main_query() ) return;
//jika admin, skip
if(is_admin()) return;
//if user is not logged in, or dont have PRIVATE_MEMBER roles hide the product
$current_user = wp_get_current_user();
if(!isset($current_user->roles) || !in_array('PRIVATE_MEMBER', $current_user->roles)){
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => 'PRIVATE_CATEGORY',
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
那代码将隐藏产品不受搜索。但是其他用户仍然可以从直接链接中访问产品细节页面。
在这里,我的代码隐藏了产品的单个页面(这是行不通的):
add_action( 'woocommerce_before_single_product', 'wcpp_hide_product_detail' );
//add_action( 'pre_get_posts', 'wcpp_hide_product_detail' );
function wcpp_hide_product_detail() {
if(is_admin()) return;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if(empty($terms)) return;
$is_private=false;
foreach($terms as $term){
if('PRIVATE_CATEGORY'==$term->term_id){
$is_private = true;
break;
}
}
if(!$is_private) return;
$current_user = wp_get_current_user();
if(!isset($current_user->roles) || !in_array('PRIVATE_MEMBER', $current_user->roles)){
//need do something here, but not works
//maybe redirect
//wp_redirect(home_url());
/* or like set 404
global $wp_query;
$wp_query->set_404();
status_header(404);
*/
}
}
如果我设置了一个重定向脚本,它会说:“不能修改标题信息--已经由.发送的标题。”
我认为,如果我可以将产品查询修改为负ID号,将触发“未找到的产品”页面,这将是可行的。但是我不知道哪个是动作钩子的名字。那么,我的代码可能可以使用wp
动作钩子(如add_action( 'wp', 'wcpp_hide_product_detail' );
)工作。但我不知道该怎么做。
任何帮助都是非常感谢的。
发布于 2019-04-13 22:33:43
我重新检查了您的代码,因为出现了一些错误(代码中的所有内容都有注释)。另外:
current_user_can()
检查当前用户的用户角色。has_term()
条件函数检查任何分类法的post术语。下面的代码将对没有特定用户角色的用户隐藏属于特定产品类别的产品。如果他们试图访问特定的产品,他们将被重定向到带有自定义通知…的商店页面。
此外,ajax按钮或"Read“按钮在该特定产品的所有产品循环中被一个禁用灰色按钮(仅在前端)替换。
以下是代码:
// Hide specific products from shop and archives pages
add_action( 'woocommerce_product_query', 'wcpp_hide_product' );
function wcpp_hide_product( $q ) {
// skip for main query and on admin
if ( ! $q->is_main_query() || is_admin() )
return;
// Hide specific products for unlogged users or users without PRIVATE_MEMBER user role
if ( is_user_logged_in() || ! current_user_can('PRIVATE_MEMBER') ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id', // it's not 'id' but 'term_id' (or 'name' or 'slug')
'terms' => 'PRIVATE_CATEGORY',
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
// Replace add to cart button by a disabled button on specific products in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'wcpp_replace_loop_add_to_cart_button', 10, 2 );
function wcpp_replace_loop_add_to_cart_button( $button, $product ) {
if ( ( is_user_logged_in() || ! current_user_can('PRIVATE_MEMBER') )
&& has_term( 'PRIVATE_CATEGORY', 'product_cat', $product->get_id() ) ) {;
$button = '' . __( "Private", "woocommerce" ) . '';
}
return $button;
}
// 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('PRIVATE_MEMBER') )
&& has_term( 'PRIVATE_CATEGORY', 'product_cat' ) && is_product() ) {
// 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
}
}
代码在您的活动子主题(或活动主题)的function.php文件中,或者在插件文件中。
测试和工作。
https://wordpress.stackexchange.com/questions/334272
复制相似问题