WooCommerce Memberships: Quickly Find Out If A User Has Memberships

Here is a quick tip to help find out if a particular user has any memberships. You can drop this function into your custom plugin or theme’s functions.php file.

<?php
/**
 * Quickly find out if a user has any membership(s)
 *
 * @param $user_id
 * @return bool
 */
function user_has_memberships( $user_id ) {
	// Retrieve user memberships
	$user_memberships = wc_memberships_get_user_memberships( $user_id );

	// Will be null if no memberships are found
	return is_null( $user_memberships) ? false : true;
}
?>

In your code, you would use it like this:

<?php
$user_id = get_current_user_id();

if( user_has_memberships( $user_id ) ) {
	echo 'User has at least one membership';
} else {
	echo 'User has no memberships';
}
?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s