SkyVerge WooCommerce Extensions

WooCommerce Memberships is designed to allow stores to have as many membership plans as desired, and in turn lets you allow customers to have more than one membership.

The plugin has conditional checks that allow you determine if a customer / user is an active member of a particular plan, as this only requires the plan ID or slug. However, checking to see if a customer / user is an active member of any plan is possible, but not as straight forward.

You can use active membership statuses to make this check a bit easier with Membership 1.4+, or you’d need to do some looping through membership plans if you use an earlier version.

This tutorial requires beginner to intermediate PHP / WordPress development skills.
Heads up! As of version 1.7, we’ve added a helper for this instead πŸ™‚ Use wc_memberships_get_user_active_memberships( $user_id )

Check for Active Memberships: WooCommerce Memberships 1.4 or Newer

In the original development of memberships, we included a function to return all of the memberships for a particular user:
wc_memberships_get_user_memberships( $user_id );

This would let you return user memberships of all statuses, and the function can accept a user id while defaulting to current user. With Memberships version 1.4+, you can also pass $args as a second parameter, which is going to make this check much more helpful for our use case.

The $args parameter currently includes an array with the membership “status” and defaults to “any” status so that, if arguments are not specified, all memberships are returned. However, this could be used to query all active memberships:

// get all active memberships for a user; 
// returns an array of active user membership objects
$user_id = get_current_user_id();

$args = array( 
    'status' => array( 'active', 'complimentary', 'pending' ),
);  

$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

(You can also add free_trial to that status array if desired and using WooCommerce Subscriptions — depends on if you want trial members counted as “active” or not.)

This returns an array of memberships that meet our arguments. In our case, the array will only contain active memberships, so if the array contains anything, we know the user is an active member.

We can then check if ( ! empty( $active_memberships ) ) to execute some code if the user is an active member.

Here’s an example that will check for inactive members, and hide the pricing display for these inactive members on purchase-restricted products.

For inactive members, the pricing display is hidden.

WooCommerce Non Member Price Display

Inactive member / non-member display

While for active members, pricing is shown. Purchasing will be disabled based on the membership rules, so if the member doesn’t have the right plan, the purchase restriction notice will still be shown.

WooCommerce Member price display

Active Member display

Check for Active Memberships: WooCommerce Memberships 1.3.x or Older

If you’re using a version of memberships older than 1.4, upgrade.

If you absolutely can’t for some reason, you could take a few additional steps to check for active memberships.

  1. First, you’d use wc_memberships_get_membership_plans() to get all plans available for your store.
  2. Next, you’d use wc_memberships_is_user_active_member() in a lop through each plan to see if your user has an active membership for that plan.

    This function checks if a user is an active member for a particular plan, so you’ll need to use the user ID and plan ID or slug:
    wc_memberships_is_user_active_member( $user_id, 'silver' );

  3. Finally, if any of the active member checks are true, you can execute your “active member” code.

// get membership plans
$plans = wc_memberships_get_membership_plans();

// set a flag
$active_member = array();

// check if the member has an active membership for any plan
foreach ( $plans as $plan ) {
    $active = wc_memberships_is_user_active_member( get_current_user_id(), $plan );
    array_push( $active_member, $active );
}

If the $active_member array contains any true value, the user is an active member.

Want to view our previous example of changing price using this older check? Here’s an example gist.

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

18 Comments

  1. Hi,

    I am working on a website https://brushbuddy.nl, aimed at consumers and retailers.
    We want a redirect function: when a customer logs in to the site, he is being redirected to the shop. If a retailer (business to business) logs in, he should be redirected to a specific productcategory, that is restricted from customers and contains products for retailers.

    I cant figure out why this function is not working:

    function my_login_redirect( $user_id ) {
    $user_id = get_current_user_id();
    if ( wc_memberships_is_user_active_member( $user_id, ‘business-to-business’ ) ) {
    return site_url( ‘/product-categorie/zakelijk/’ );
    }
    else { return site_url( ‘/shop/’ ); }
    }
    add_filter( ‘login_redirect’, ‘my_login_redirect’, 10, 3 );

  2. Hi,

    Im trying to use the same concept as above but create some conditional statement within my template. E.G. If users membership status is active, show this content, else if status is cancelled show this content.

    Below is what i have but cant seem to get it to work…

    $user_id = get_current_user_id();
    $args = array(
    ‘status’ => array( ‘wcm-active’, ‘wcm-delayed’, ‘wcm-complimentary’, ‘wcm-pending’, ‘wcm-paused’, ‘wcm-expired’, ‘wcm-cancelled’ ),
    );
    if (!empty($active_memberships)) {
    // show content if the current user has any memberships

    if ( in_array(‘wcm-active’, $active_memberships) ) {
    echo ‘active membership’;
    }

    if ( in_array(‘wcm-delayed’, $active_memberships) ) {
    echo ‘delayed membership’;
    }
    }

    • Hey Will, (1) you don’t need wcm- prefixes, (2) $active_memberships isn’t currently set anywhere, you need to get the memberships with those statuses first.

      • Hi Beka,

        Thanks for getting back to me. Sorry, i did have the $active_memberships set but didnt include it (not helpful). I did try without the wcm- as well but no luck.
        When i var_dump the $active_memberships it showed the status string as wcm-active which was why i included it.

        Below is what i have now, but still no luck! Really cant seem to figure this one out!

        $user_id = get_current_user_id();
        $args = array(
        ‘status’ => array( ‘active’, ‘delayed’, ‘complimentary’, ‘pending’, ‘paused’, ‘expired’, ‘cancelled’ ),
        );
        $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

        if (!empty($active_memberships)) {
        // show content if the current user has any memberships

        if ( in_array(‘active’, $active_memberships) ) {
        // show content if ‘wcm-active’ is present in the active_memberships
        echo ‘active membership’;
        }

        elseif ( in_array(‘delayed’, $active_memberships) ) {
        // show content if ‘wcm-delayed’ is present in the active_memberships
        echo ‘delayed membership’;
        }

        else {
        echo ‘none’;
        }

        }

        • Hey Will, I meant that your ‘status’ array didn’t need the prefix in the $args. Now that I see what you used to get active memberships, note that this doesn’t give you an array of statuses — it gives you an array of user membership objects, so something like in_array(β€˜active’, $active_memberships) can’t work, nor can you do this without looping the active memberships or plucking statuses of all memberships into a new array. I’d recommend reaching out to someone at Codeable for assistance with this one.

  3. Hi!

    Is there a way to display a list of active members publicly on the website. Kind of like a member directory?

  4. Hello Beka

    Thank you for this tutoriel, very userful

    However i need to achieve something and maybe you can help me to go in the right direction

    I need to display in a page all users who have an active membership for a minimum of 3 months consecutively nonstop (no pause)

    Any help would be apreciate

    Cheers

  5. Beka,

    This is a great tutorial, but I am hoping you can clarify something for me. Can’t this be done with the “wc_memberships_my_account_my_memberships_actions” filter? Taking some clues from your “Renew with a Different Plan” post, my idea was to use it like this…

    $membership = apply_filters(‘wc_memberships_my_account_my_memberships_actions’, $actions, $memberships )

    and then do some checks against the object:

    if ( $membership->is_expired() ) {
    // Do good stuff
    }

    I tried this approach first, and I can’t seem to get it to work. Problem is that the object does not seem to be getting populated and it’s returning null. Perhaps I am confused about how it is called? I noticed you are not using the apply_filters function above to create your $active_memberships object above. Also, I can’t seem to find wc_memberships_my_account_my_memberships_actions in any of the documentation and has me wondering if it has since been deprecated. I should also not that I am not using the my-account “Woo” login in lieu of the one from Profile Builder, hence my using the apply_filters function.

    Please forgive my ignorance here if that’s really the problem. I am new to coding WordPress and PHP. That said, your posts continue to be helpful and very educational!

    • What should I select when I go to this link, I have options like – my account, woocommerce, woocommerce extensions and non woocommerce plugins?

  6. Hi there, I have a question.

    Is there away to hide some memberships from my membership area?
    I have a membership that never expires, is it possible to hide it??

    Thanks
    Miquel

Hmm, looks like this article is over 2 years old! Its content may be outdated, so comments are now closed.