Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created July 27, 2016 17:28
Show Gist options
  • Save bekarice/71a4199f65becaae68a6caf5511478a1 to your computer and use it in GitHub Desktop.
Save bekarice/71a4199f65becaae68a6caf5511478a1 to your computer and use it in GitHub Desktop.
Get active memberships for a user with WC Memberships
<?php
/**
* Helper function to get all active memberships for a user
*
* @param int|\WP_User $user_id Optional, defaults to current user
* @return array - empty array|array of active memberships for the user
*/
function sv_wc_memberships_get_active_memberships( $user_id = null ) {
if ( null === $user_id ) {
$user_id = get_current_user_id();
} elseif ( isset( $user_id->ID ) ) {
$user_id = $user_id->ID;
}
// bail if this is user is not logged in
if ( ! is_numeric( $user_id ) || 0 === $user_id ) {
return array();
} else {
$user_id = (int) $user_id;
}
$args = array(
'status' => array( 'active', 'complimentary', 'pending', 'free-trial' ),
);
return wc_memberships_get_user_memberships( $user_id, $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment