0

WooCommerce 会员资格有一些条件,但我不能将它们结合起来专门测试特定成员是否可以访问特定内容。

 $post_id = get_the_ID(); // this post is available to access level 'insights' but *not* available to a member with access level 'resources'

 // if post content is restricted
 if ( wc_memberships_is_post_content_restricted($post_id) ) {
 // returns true for *any* restricted content (whether restricted for this member or not)

     // check if user has membership
     $user_id = get_current_user_id(); // this member has access level 'resources' but not 'insights'
     if ( wc_memberships_is_user_active_member( $user_id, 'resources' ) ){
     /// will return *true* dependent on user but *not* dependent on content
         echo "You";
     } else {
         echo 'Not you ';
     }
 }

总是返回“你”

那么如何检查此用户是否具有内容的访问级别?

用简单的英语,我想我需要找出对帖子内容施加了哪些限制,但似乎没有条件(这很奇怪)

4

1 回答 1

1

在房子周围转了几圈后,在天空边缘的帮助下

function wc_memberships_user_has_access($post_id) {

  if (wc_memberships_is_post_content_restricted() && ! current_user_can( 'wc_memberships_view_restricted_post_content', $post_id ))  {
    return false;
  } else {
    return true;
  }

}

只需将帖子 ID 传递给此函数,它会根据当前用户对当前帖子的访问权限返回一个布尔值。

于 2020-09-09T17:31:17.720 回答