How To Start a Service Business Computer

WooCommerce Memberships: check for any active membership

CommerceCategory
5 min read
Beka Rice

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.

<?php
/**
 * Modify membership product pricing display for non-members
 * changes pricing display if purchasing is restricted to members;
 * active members will see the price instead of a message
 *
 * @param string $price the WC price HTML
 * @return string $price the updated price HTML
 */
function sv_change_member_product_price_display( $price ) {

	// bail if Memberships isn't active
	if ( ! function_exists( 'wc_memberships' ) ) {
		return $price;
	}

	// get any active user memberships (requires Memberships 1.4+)
	$user_id = get_current_user_id();
	$args = array( 
		'status' => array( 'active', 'complimentary', 'pending' ),
	);
	
	$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

	// only proceed if the user has no active memberships
	if ( empty( $active_memberships ) ) {
	
		// change price display if purchasing is restricted
		if ( wc_memberships_is_product_purchasing_restricted() ) {
			$price = 'Price for members only';
		} 
	}

	return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_member_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'sv_change_member_product_price_display' );

For inactive members, the pricing display is hidden.

WooCommerce Non Member Price 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

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.