Membership API Query

I am trying to understand how to work with the M2 API. I just need to figure out how I can check Membership Attributes, Membership Status of user and verify membership status.

Looking for a solution as like mentioned in the following article:
https://premium.wpmudev.org/forums/topic/membership-2-api-implementation-help?replies=5#post-1004913

So, it would be easier to call the attributes, like for example:
$_m2_status = $member->status;
$_m2_attributes = $member->attributes;

If you could suggest an example on how to call the methods as requested above, it would be helpful.

  • Panos

    Hi Michael ,

    Not that simple to include everything in a forum answer as Membership plugin has many features. I'll try include main stuff here hoping it would be helpful for your project :slight_smile:

    Basic thing to know is that Members use Subscriptions to connect to a Membership. So a Member has a Subscription for a Membership, and it's the Subscription that has a status, expiry date etc.

    Here are some methods that you may need:

    For a given user_id:
    $member = MS_Factory::load( 'MS_Model_Member', $user_id );

    For current user:
    $member = MS_Model_Member::get_current_member();

    If you have subscription:
    $member = $subscription->get_member();

    Go through member memberships:

    $memberships_ids 	= ( array ) $member->get_membership_ids();
    foreach( $membership_ids as $membership_id ) {
    	$membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    }

    Membership name:
    $membership->name

    Get subscriptions based on user_id:

    $subscriptions = MS_Model_Relationship::get_subscriptions(
    	array(
    		'user_id' 	=> $user_id,
    		'status' 	=> 'all',
    	)
    );

    Get subscriptions of a membership id:

    $subscriptions = MS_Model_Relationship::get_subscriptions(
    	array( 'membership_id' => $membership->id )
    );

    Get subscription status:
    $subscription->get_status()

    Subscription start date:
    $subscription->start_date

    Is subscription expired:
    $subscription->is_expired()

    Get membership of a subscription:
    $subscription->get_membership()

    get subscription gateway:
    $subscription->get_gateway();

    Get subscription current invoice:
    $invoice = $subscription->get_current_invoice();

    Is invoice paid:
    $invoice->is_paid()

    Invoice total:
    $invoice->total

    Invoice due date:
    $invoice->due_date

    Hope this helps :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.