WooCommerce Memberships: Conditional to check a page access

别说谁变了你拦得住时间么 提交于 2020-01-02 19:47:13

问题


I have a Wordpress Memberships website that is built on WooCommerce with WooCommerce Memberships plugin to restrict certain pages to members only.

Some of those pages are "drip-fed"... ie. Access to those pages opens 3 days after purchase, etc. I have set this up in WooMemberships.

I am trying to simply do a PHP conditional check to see if the current user has access to a certain page.

I have found this code piece in the docs: wc_memberships_is_post_content_restricted()

However, I have been unable to make it work.

Is there a code snippet which will basically do a PHP IF statement on whether the current user has access to a certain page (using page ID)?

eg:

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

Thanks.


回答1:


You will have to Replace (in the conditions):

  1. $page_id by your page ID number (for example: is_page(42))
  2. $membership_plan by the slug of the plan ('plan_slug') or related post ID.

The conditions:

  • wc_memberships_is_post_content_restricted($page_id) => true if $page_id is retracted.
  • is_page($page_id) => true if is actual $page_id.
  • wc_memberships_is_user_active_member( $membership_plan ) => true actual user is an active member for this $membership_plan plan. In that case the access to the page is granted by the suscription plan of the user.

You can remove some of the conditions, if not needed, and fine tune for your needs.

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

    // do something

} else {

    // don't

}

--- Update ---

The only function related to restriction and (or) time access are:

1) wc_memberships_restrict( $content, $membership_plans, $delay, $exclude_trial )
just like shortcode [wcm_restrict] (so not useful)…

2) wc_memberships_get_user_access_time( $user_id, $target, $action, $gmt ): Parameters

$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

Reference: WooCommerce Memberships Function Reference




回答2:


I'm dealing with the same issue at StoryMoment.com (we produce audio + eBook story series for kids).

This is how I've dealt with it. I use the following in a page template to show or hide page elements based on access. The wc_memberships_view_delayed_post_content would change based on the type of content.

You can see the other options in the file:

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

}

?>



回答3:


I'm not sure if this helps but here is my take on it. I first go through all of the users active membership and then I check the content $rules to see if the restricted plans are a part of the users membership plans (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;
}

Usage would be something like:

if(can_user_access_content(get_current_user_id(),$post->ID)){
    //do whatever here
}


来源:https://stackoverflow.com/questions/37714426/woocommerce-memberships-conditional-to-check-a-page-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!