WooCommerce 会员资格检查特定用户是否能够访问特定内容

WooCommerce Memberships check if a specific user is able to access a specific content

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 ';
     }
 }

总是returns'You'

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

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

在 sky verge 的帮助下绕过房子几次后

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;
  }

}

只需将 post ID 传递给此函数,它 returns 一个布尔值,具体取决于当前用户对当前 post.

的访问权限