【问题标题】:WooCommerce Memberships check if a specific user is able to access a specific contentWooCommerce 会员资格检查特定用户是否能够访问特定内容
【发布时间】:2020-12-08 22:09:42
【问题描述】:

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

总是返回“你”

那么如何检查用户是否具有内容的访问权限?

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

【问题讨论】:

    标签: woocommerce woocommerce-memberships


    【解决方案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 传递给此函数,它会根据当前用户对当前帖子的访问权限返回一个布尔值。

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2016-10-08
      • 2022-08-22
      • 2011-02-18
      • 1970-01-01
      • 2016-10-09
      • 2016-12-10
      • 1970-01-01
      相关资源
      最近更新 更多