WooCommerce 会员资格检查用户(具有当前会员计划)是不是能够访问内容

Posted

技术标签:

【中文标题】WooCommerce 会员资格检查用户(具有当前会员计划)是不是能够访问内容【英文标题】:WooCommerce Memberships check if user (with current membership plan) is able to access the contentsWooCommerce 会员资格检查用户(具有当前会员计划)是否能够访问内容 【发布时间】:2017-02-04 13:06:15 【问题描述】:

目前,我正在尝试检查用户是否有权访问某个页面(基于他们的会员计划)。下面是我的代码,但似乎 wc_memberships_is_user_active_member 只检查用户是否是活跃成员。

if( wc_memberships_is_post_content_restricted() && is_page($postid) && wc_memberships_is_user_active_member( $membership_plan ) ) 

//do something

 else 

//do something


提前致谢。

【问题讨论】:

【参考方案1】:

我设法用下面的代码做到了,它检查用户(具有当前会员资格)是否能够访问该页面:

function can_user_access_content($user_id,$post_id)
    //check if there's a force public on this content    
    if(get_post_meta($post_id,'_wc_memberships_force_public',true)=='yes') return true;
    $args = array( 'status' => array( 'active' ));
    $plans = wc_memberships_get_user_memberships( $user_id, $args );
    $user_plans = array();
    foreach($plans as $plan)
        array_push($user_plans,$plan->plan_id);
    
    $rules = wc_memberships()->get_rules_instance()->get_post_content_restriction_rules( $post_id );

    foreach($rules as $rule)
        if(in_array($rule->get_membership_plan_id(), $user_plans))
            return true;
        
           
    return false;


if(can_user_access_content(get_current_user_id(),$post->ID))
    //do something
 else 
    //do something

Paulo 提供的答案: WooCommerce Memberships: Conditional to check a page access

【讨论】:

【参考方案2】:

According to the documentation您可以传入特定的用户ID和计划名称,以便在查看此页面时检查用户是否在特定计划中。

例如:

if (wc_memberships_is_user_active_member($user_id, 'silver_plan') 
    // show content
 else 
   // show sign up form

【讨论】:

【参考方案3】:
<?php
$curr_user_id = get_current_user_id();

$memberships = array( 'plan-slug-1', 'plan-slug-2', 'plan-slug-3' );

if ( wc_memberships_get_user_memberships( $curr_user_id, $memberships ) )  
    echo "You are a member so you can view this content";
 else 
    echo "Become a member to view this content";

【讨论】:

【参考方案4】:
<?php
// get all active memberships for a user; 
// returns an array of active user membership objects
// or null if no memberships are found
$user_id = get_current_user_id();
$args = array( 
    'status' => array( 'active', 'complimentary', 'pending' ),
);  
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
if ( ! empty( $active_memberships ) ) 
echo "User is active";

?>

【讨论】:

谢谢先生,真的很有帮助。【参考方案5】:

我在 content-product.php 页面上的 wordpress 上有类似的问题

这就是我要给出的内容...遗憾的是没有工作...知道我可以在那里使用什么来检查我的成员是否获得了他们的计划并显示价格吗?

首价不打折。第二个价格是vip折扣。

<?php  
                        if (wc_memberships_is_user_active_member($user_id, 'pack-vip') 
                        echo $price_val, $symbol    
                         else 
                        echo $price_val - $vip_discount, $symbol    
                        

所以我尝试了一些替代 echo...

                        //print / printf /printr doesn't work
                        //return (doesn't work)
                        //echo (doesn't work)
                        //empty before $price_val (doesn't work)

                        //What i want to deliver is something like 
                        //echo $price_val - $vip_discount, $symbol // 
                        //Without if or else, works fine but only for 
                        //users without the pack VIP.
                        //For users with pack VIP, it reduces the prices 
                        //again on it
                        //Solution, need to split with if else in content- 
                        //product.php to verify
                        //If user is active member of 'pack-vip' (slug) 
                        //Display the price without $vip_discount 
                        
                    ?>

【讨论】:

您好,欢迎来到 ***!您的帖子没有提供和回答问题。请考虑发布您自己的问题,并在how to write a good answer 上查看这里,谢谢!

以上是关于WooCommerce 会员资格检查用户(具有当前会员计划)是不是能够访问内容的主要内容,如果未能解决你的问题,请参考以下文章

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP

php 兼容多厂商市场的WooCommerce会员资格

php WooCommerce会员 - 检查用户是否是计划的成员

php WooCommerce会员 - 检查用户是否是计划的成员