doudi5524 2016-09-27 03:30
浏览 128
已采纳

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

Currently I am trying to check if the user has the access to certain page (based on their membership plan). Below is my code, but it seems like wc_memberships_is_user_active_member only check if the user is an 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

}

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • douji1853 2016-09-27 03:55
    关注

    I managed to do it with the code below, it check whether if the user (with current membership) is able to access the page:

    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
    }
    

    Answer provided by Paulo: WooCommerce Memberships: Conditional to check a page access

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私