Scott Scott - 5 months ago 9
PHP Question

WooCommerce Memberships: Conditional Restricted Content Check

I'm trying to make a menu of pages links that people can access once they have purchased a membership (through WooCommerce Memberships). I want for that pages to be greyed out, if they don't yet have access to them.

I've set the pages to gain access X days after purchase in WooCommerce Memberships, however the code I am using is simply not working. It is returning false even when it should return true.

Any ideas?

<?php
$restrict_th0 = wc_memberships_is_post_content_restricted(24);
$restrict_th1 = wc_memberships_is_post_content_restricted(28);
$restrict_th2 = wc_memberships_is_post_content_restricted(30);
?>

<ul id="menu-academy">
<li>
<?php if ( $restrict_th0 ) { } else { ?>
<a href="<?php echo home_url(); ?>/URL HERE/">
<?php } ?>
<span class="module_no">0</span><span class="module_descript">MODULE DESCRIPTION<span class="module_access<?php if ( $restrict_th0 ) { ?> lock<?php } ?>">
<i class="fa fa-2x fa-<?php if ( $restrict_th0 ) { } else { ?>un<?php } ?>lock<?php if ( $restrict_th0 ) { } else { ?>-alt<?php } ?>" aria-hidden="true"></i></span></span><?php if ( $restrict_th0 ) { } else { ?></a><?php } ?></li>

</ul

Answer

You should better use this conditional :

if( wc_memberships_is_user_active_member( $membership_plan ) ) {
   // Displayed fully functional Menu
} else {
   // Greyed displayed inactive Menu
}

This works for active logged user that have subscribed to a plan and which subscription is valid.

$membership_plan has to be replaced by a membership plan slug, post object or related post ID.

After you can use also wc_memberships_is_post_content_restricted(ID) or is_page(ID) for furthers actions…