4

Currently, I am trying to check if the user has the access to a certain page (based on their membership plan). Below is my code, but it seems like wc_memberships_is_user_active_member only checks if the user is an active member.

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

//do something

} else {

//do something

}

Thanks in advance.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Lim Zhiyang
  • 91
  • 1
  • 6

5 Answers5

5

I managed to do it with the code below, it check whether if the user (with current membership) is able to access the page:

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 something
} else {
    //do something
}

Answer provided by Paulo: WooCommerce Memberships: Conditional to check a page access

Community
  • 1
  • 1
Lim Zhiyang
  • 91
  • 1
  • 6
3

According to the documentation you can pass in a specific user id and plan name in order to check if a user is on a specific plan when viewing this page.

For example:

if (wc_memberships_is_user_active_member($user_id, 'silver_plan') {
    // show content
} else {
   // show sign up form
}
jostrander
  • 745
  • 5
  • 18
3
<?php
$curr_user_id = get_current_user_id();

$memberships = array( 'plan-slug-1', 'plan-slug-2', 'plan-slug-3' );

if ( wc_memberships_get_user_memberships( $curr_user_id, $memberships ) ) { 
    echo "You are a member so you can view this content";
} else {
    echo "Become a member to view this content";
}
Seher Khan
  • 31
  • 1
1
<?php
// get all active memberships for a user; 
// returns an array of active user membership objects
// or null if no memberships are found
$user_id = get_current_user_id();
$args = array( 
    'status' => array( 'active', 'complimentary', 'pending' ),
);  
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
if ( ! empty( $active_memberships ) ) {
echo "User is active";
}
?>
Maulik patel
  • 2,546
  • 2
  • 20
  • 26
0

I have a similar issue on a wordpress on the content-product.php page

Here is what I'm trying to give out... sadly ain't working... any idea what i could use there to check if my members got their plan and show the price in function of that?

The first price is without discount. The second price is with vip discount.

<?php  
                        if (wc_memberships_is_user_active_member($user_id, 'pack-vip') {
                        echo $price_val, $symbol    
                        } else {
                        echo $price_val - $vip_discount, $symbol    
                        }

So I have try with a few alternatives to echo...

                        //print / printf /printr doesn't work
                        //return (doesn't work)
                        //echo (doesn't work)
                        //empty before $price_val (doesn't work)

                        //What i want to deliver is something like 
                        //echo $price_val - $vip_discount, $symbol // 
                        //Without if or else, works fine but only for 
                        //users without the pack VIP.
                        //For users with pack VIP, it reduces the prices 
                        //again on it
                        //Solution, need to split with if else in content- 
                        //product.php to verify
                        //If user is active member of 'pack-vip' (slug) 
                        //Display the price without $vip_discount 
                        
                    ?>
fxthtqkc
  • 1
  • 1
  • 1
    Hi and Welcome to StackOverflow! Your post does not provide and answer to the question. Please consider posting your own question, and take a look here on [how to write a good answer](https://stackoverflow.com/help/how-to-answer), thanks! – BiOS Mar 03 '21 at 07:43