WooCommerce Memberships: Useful Functions To Know

If you are are just starting to work with WooCommerce Memberships and are having to do various things programatically, here are some functions that are useful to know and will make your life and job a lot easier.

Retrieve User’s Memberships

Retrieve a list of memberships this particular user belongs to. This function will return an array of WC_Memberships_User_Membership objects or NULL if no memberships are found for the user.

<?php
// Get the user ID
$user_id = get_current_user_id();
// Get memberships
$user_memberships = wc_memberships_get_user_memberships( $user_id );
?>

Check If User Is An Active Member Of A Particular Membership Plan

Use wc_memberships_is_user_active_member() to quickly figure out if a user is an active member of any of your membership plans

<?php
// Get the user ID
$user_id = get_current_user_id();
// Plan ID
$plan_id = 25;

if( wc_memberships_is_user_active_member( $user_id, $plan_id ) ) {
	echo 'User has active membership';
} else {
	echo 'User is not an active member of this membership';
}
?>

Check If User Is A Member Of A Particular Membership Plan

This function is similar to the the previous one, but it doesn’t not take into consideration the “Active” status.

<?php
$user_id = get_current_user_id();
$plan_id = 26560;

if( wc_memberships_is_user_member( $user_id, $plan_id ) ) {
	echo 'User is a member of this membership plan';
} else {
	echo 'User is not a member of this membership plan';
}
?>

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