将产品限制为特定用户角色
我有一个只有两个产品的WooCommerce网站,每种产品都属于其类别;
类别X
必须每个人都能看到。
类别y
必须仅限于具有用户角色的用户:从业者。
我如何:
(1)使类别y仅对从业者
用户角色可见
,并且
(2)使所有人都可以看到类别,但只能通过从业者
用户角色购买?
这是我的尝试,尽管无法正常工作。
add_action( 'woocommerce_product_query', 'limit_products_basic', 10, 2 );
function limit_products_basic( $q, $query )
{
$user = wp_get_current_user();
if ( !in_array( "practitioner", $user->roles ) ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'app' ),
'include_children' => true,
);
$q->set( 'tax_query', $tax_query );
}
}
我知道有很多插件可以帮助实现这一目标,但是我希望能保持清洁和“简单”。
谢谢
I have a woocommerce site with two products only, each belonging to their category;
Category X
must be visible by everyone.
Category Y
must be limited to users with the user role: practitioner.
How can I:
(1) Make Category Y only visible to practitioner
user roles
and
(2) Make Category Y visible to all, but only purchasable by practitioner
user role?
This is my attempt though not working ar expected.
add_action( 'woocommerce_product_query', 'limit_products_basic', 10, 2 );
function limit_products_basic( $q, $query )
{
$user = wp_get_current_user();
if ( !in_array( "practitioner", $user->roles ) ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'app' ),
'include_children' => true,
);
$q->set( 'tax_query', $tax_query );
}
}
I know there are lots of plugins that can help to achieve this, but I am hoping to keep it clean and 'simple'.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了Tom Anbinder的这个出色的教程,这就是结果:
汤姆网站上的更多详细信息: wpfactory.com
I found this brilliant tutorial by Tom Anbinder and this is the result:
More detail on tom's website: wpfactory.com