WooCommerce会员资格检查用户(具有当前会员计划)是否能够访问内容 [英] WooCommerce Memberships check if user (with current membership plan) is able to access the contents

查看:111
本文介绍了WooCommerce会员资格检查用户(具有当前会员计划)是否能够访问内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

请先谢谢。

推荐答案

我设法使用下面的代码来完成此任务,它检查用户(具有当前成员身份)是否能够访问页面: / p>

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
}

Paulo提供的答案:
WooCommerce成员资格:有条件检查页面访问权限

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

这篇关于WooCommerce会员资格检查用户(具有当前会员计划)是否能够访问内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆