将产品限制为特定用户角色

发布于 2025-02-01 11:24:59 字数 818 浏览 2 评论 0原文

我有一个只有两个产品的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2025-02-08 11:24:59

我发现了Tom Anbinder的这个出色的教程,这就是结果:

add_role( 'practitioner', 'Practitioner', get_role( 'customer' )->capabilities );

if ( ! function_exists( 'wpf_is_current_user_role' ) ) {
    function wpf_is_current_user_role( $roles_to_check ) {
        $current_user       = wp_get_current_user();
        $current_user_roles = ( empty( $current_user->roles ) ? array( '' ) : $current_user->roles );
        $roles_intersect    = array_intersect( $current_user_roles, $roles_to_check );
        return ( ! empty( $roles_intersect ) );
    }
}

if ( ! function_exists( 'wpf_do_hide_product' ) ) {

    function wpf_do_hide_product( $product_id_to_check ) {

        $products_to_hide  = array( 204,); 
        $roles_to_hide_for = array( 'practitioner' ); 

        return (
            in_array( $product_id_to_check, $products_to_hide ) && 
            !wpf_is_current_user_role( $roles_to_hide_for )         
        );
    }
}

add_filter( 'woocommerce_is_purchasable', 'wpf_product_purchasable_by_user_role', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpf_product_purchasable_by_user_role' ) ) {
    function wpf_product_purchasable_by_user_role( $purchasable, $product ) {
        return ( wpf_do_hide_product( $product->get_id() ) ? false : $purchasable );
    }
}

汤姆网站上的更多详细信息: wpfactory.com

I found this brilliant tutorial by Tom Anbinder and this is the result:

add_role( 'practitioner', 'Practitioner', get_role( 'customer' )->capabilities );

if ( ! function_exists( 'wpf_is_current_user_role' ) ) {
    function wpf_is_current_user_role( $roles_to_check ) {
        $current_user       = wp_get_current_user();
        $current_user_roles = ( empty( $current_user->roles ) ? array( '' ) : $current_user->roles );
        $roles_intersect    = array_intersect( $current_user_roles, $roles_to_check );
        return ( ! empty( $roles_intersect ) );
    }
}

if ( ! function_exists( 'wpf_do_hide_product' ) ) {

    function wpf_do_hide_product( $product_id_to_check ) {

        $products_to_hide  = array( 204,); 
        $roles_to_hide_for = array( 'practitioner' ); 

        return (
            in_array( $product_id_to_check, $products_to_hide ) && 
            !wpf_is_current_user_role( $roles_to_hide_for )         
        );
    }
}

add_filter( 'woocommerce_is_purchasable', 'wpf_product_purchasable_by_user_role', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpf_product_purchasable_by_user_role' ) ) {
    function wpf_product_purchasable_by_user_role( $purchasable, $product ) {
        return ( wpf_do_hide_product( $product->get_id() ) ? false : $purchasable );
    }
}

More detail on tom's website: wpfactory.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文