【问题标题】:WooCommerce Memberships: Conditional to check a page accessWooCommerce 会员资格:有条件检查页面访问权限
【发布时间】:2016-10-09 10:13:00
【问题描述】:

我有一个基于 WooCommerce 的 Wordpress 会员网站,带有 WooCommerce 会员插件,可将某些页面仅限于会员。

其中一些页面是“滴灌式”......即。购买后 3 天即可访问这些页面,等等。我已在 WooMemberships 中进行了设置。

我正在尝试简单地进行 PHP 条件检查以查看当前用户是否可以访问某个页面。

我在文档中找到了这段代码:wc_memberships_is_post_content_restricted()

但是,我无法让它工作。

是否有代码 sn-p 基本上会针对当前用户是否有权访问某个页面(使用page ID)执行 PHP IF 语句?

例如:

if ( current_user_has_access(page_ID) ) { DO SOMETHING } else { DON'T }

谢谢。

【问题讨论】:

  • 谢谢你:)

标签: php wordpress woocommerce conditional woocommerce-memberships


【解决方案1】:

您必须替换(在条件下):

  1. $page_id 由您的page ID 编号(例如:is_page(42))
  2. $membership_plan 由计划的 slug ('plan_slug') 或相关的post ID

条件:

  • wc_memberships_is_post_content_restricted($page_id) => true 如果$page_id 被收回。
  • is_page($page_id) => 如果是实际 $page_id,则为真。
  • wc_memberships_is_user_active_member( $membership_plan ) => true 实际用户是此$membership_plan 计划的活跃成员。在这种情况下,该页面的访问权限由用户的订阅计划授予。

如果不需要,您可以删除一些条件,并根据您的需要进行微调。

if( wc_memberships_is_post_content_restricted() && is_page($page_id) && wc_memberships_is_user_active_member( $membership_plan ) ) {

    // do something

} else {

    // don't

}

--- 更新---

与限制和(或)时间访问相关的唯一功能是:

1) wc_memberships_restrict( $content, $membership_plans, $delay, $exclude_trial )
就像简码 [wcm_restrict](所以没用)...

2) wc_memberships_get_user_access_time( $user_id, $target, $action, $gmt ):参数

$user_id  // for a logged 'user ID'
$target   : array('post' => id, 'product' => id) // content_type and content_id
$action   : 'view' or 'purchase' // Type of access (products only)<br>
$gmt =>   : true  or  false // (selection of the time zone)
// Returns user access start timestamp (in site timezone) for content or a product

参考:WooCommerce Memberships Function Reference

【讨论】:

  • 太棒了,谢谢。我已经尝试过了,它正在确认用户是会员等 - 但是它说他们可以访问在 X 天过去之前他们不应该访问的页面......有没有办法检查会员可以根据他们的注册日期访问某个页面(我在 Woo Memberships 中设置了页面应该只在 X 天后显示)??对于上下文 - 我正在创建一个仅显示他们当前有权访问的页面的菜单。
【解决方案2】:

我不确定这是否有帮助,但这是我的看法。我首先检查所有用户的活动成员资格,然后检查内容 $rules 以查看受限计划是否是用户成员资格计划的一部分(in_array)

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 whatever here
}

【讨论】:

  • 如果内容完全被限制,我添加了:function can_user_access_content($user_id,$post_id){ //Check if this is restricted at all if(wc_memberships_is_post_content_restricted($post_id) === false ) return true; }
【解决方案3】:

我在 StoryMoment.com 处理同样的问题(我们为儿童制作音频 + 电子书故事系列)。

这就是我的处理方式。我在页面模板中使用以下内容根据访问权限显示或隐藏页面元素。 wc_memberships_view_delayed_post_content 会根据内容的类型而改变。

你可以在文件中看到其他选项:

class-wc-memberships-capabilities.php

<?php 

$has_access = current_user_can( 'wc_memberships_view_delayed_post_content', $post->ID );

if ($has_access) {

//do something

} else {

//do something else

}

?>

【讨论】:

  • 这正是有效的方法,易于实施!谢谢!
  • 同意。这非常简单,而且效果很好。只是想知道如何/是否可以将“受限内容”消息中内置的会员资格添加到 else 部分?
猜你喜欢
  • 2016-10-08
  • 2017-02-04
  • 2022-08-22
  • 2016-11-02
  • 2018-12-20
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多