WooCommerce Memberships: Conditional to check a page access WooCommerce Memberships: Conditional to check a page access wordpress wordpress

WooCommerce Memberships: Conditional to check a page access


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


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}


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