Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCoomerce会员资格-管理员当前没有会员资格_Php_Wordpress_Woocommerce - Fatal编程技术网

Php WooCoomerce会员资格-管理员当前没有会员资格

Php WooCoomerce会员资格-管理员当前没有会员资格,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试将WooCommerce会员资格与一些自定义高级自定义字段代码和Wordpress设置相集成。我找到了一个用于检查用户是否可以访问内容的方法。不幸的是,当我的管理员角色甚至没有访问权限时,我遇到了一些困难。在我的调试中,我发现当使用当前用户ID调用wc\u memberships\u get\u user\u memberships时,我的管理员用户将返回NULL。此后,我添加了一个检查,以查看用户是否是商店经理或管理员,并始终为他们返回true function can_user_a

我正在尝试将WooCommerce会员资格与一些自定义高级自定义字段代码和Wordpress设置相集成。我找到了一个用于检查用户是否可以访问内容的方法。不幸的是,当我的管理员角色甚至没有访问权限时,我遇到了一些困难。在我的调试中,我发现当使用当前用户ID调用
wc\u memberships\u get\u user\u memberships
时,我的管理员用户将返回NULL。此后,我添加了一个检查,以查看用户是否是商店经理或管理员,并始终为他们返回true

function can_user_access_content($user_id,$post_id){

    // bail if Memberships isn't active
    if ( ! function_exists( 'wc_memberships' ) ) {
        return true;
    }

    $user = get_user_by('id',$user_id);
    if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'shop_manager', (array) $user->roles ) ) {
        //The user has the "administrator" or "shop manager" role
        return true;
    }

    //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 );

    if(!count($rules)) {
        return true;
    }

    foreach($rules as $rule){
        if(in_array($rule->get_membership_plan_id(), $user_plans)){
            return true;
        }
    }
    return false;
}
这是实现这一目标的正确方法吗?我还在努力寻找一种方法来检索页面/帖子/产品要显示的适当的“内容不可用”消息。如果内容受到限制,我会退回到显示常规帖子内容,这通常会在我的WC成员身份中过滤消息,但这似乎并不总是有效。我会喜欢更简单一点的


任何帮助都将不胜感激

插件已经为此提供了一个功能:

他们似乎已经处理了管理员逻辑,因为在

作为站点上的管理员(或商店经理)用户,没有内容 将仅限于您,以便您可以轻松管理您的网站


我直接联系了WooCoomerce开发人员,得到了如下回复:

我觉得你的方法不错。不幸的是,你是对的 不是一个内置的公共函数,用于检查当前 当前用户可以访问内容。这需要写出来 作为一个自定义函数,正如您在这里所做的那样

可能有其他方法可以通过以下组合实现这一点:
wc\u成员身份\u获取用户\u成员身份()
wc\u memberships\u是\u user\u active\u member()
照我看还可以


因此,上述方法似乎还可以。

谢谢Turtletrade-此功能实际上只会告诉您产品是否有任何限制-而不是当前用户是否被允许查看它-我们多少需要自己检查一下。
function wc_memberships_is_product_viewing_restricted( $post_id = null )