Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Created March 7, 2018 15:51
Show Gist options
  • Save cartpauj/be4c2eb63432e8a8942a8fa4cc321f3b to your computer and use it in GitHub Desktop.
Save cartpauj/be4c2eb63432e8a8942a8fa4cc321f3b to your computer and use it in GitHub Desktop.
Get a list of the current user's active MemberPress Subscriptions
<?php
if(class_exists('MeprUtils')) {
$user = MeprUtils::get_currentuserinfo();
if($user !== false && isset($user->ID)) {
//Returns an array of Membership ID's that the current user is active on
//Can also use 'products' or 'transactions' as the argument type
$active_prodcuts = $user->active_product_subscriptions('ids');
if(!empty($active_prodcuts)) {
foreach($active_prodcuts as $membership_id) {
// Do something
}
}
}
}
@mdahlke
Copy link

mdahlke commented Sep 19, 2018

This is a better way, IMHO, because it uses MeprUser class.
I've wrapped it in a function that can be used to get a specific user's memberships using $user_id:
https://gist.github.com/dev-masta/dcc7d6452182ec0161ebd2a06f66c4ed

The function returns an array of membership IDs.

If you look into the MeprUtils::get_currentuserinfo() method, you'll see that it uses the MeprUser class.
I think this approach works just fine.
That is unless you want to get the subscriptions of a different user than the one currently logged in. In which, your function handles that nicely. But as this snippet's description says, it will "Get a list of the current user's active MemberPress Subscriptions", I think this is the better way.

Copy link

ghost commented Dec 19, 2018

Hi there. Thank you for sharing this. Would you mind elaborating on how I would then dive into each active product/subscription? The foreach you have retrieves the id but i'm hoping to grab the created on/expires on dates. I have played around with it myself but I'm definitely missing a key step.

function get_expiry(){
$user = MeprUtils::get_currentuserinfo();
$subscriptions = $user->active_product_subscriptions();
	
	if(!empty($subscriptions)) {
        foreach($subscriptions as $s):
                $expires_at= $s->subscr_expires_at();
                echo  $expires_at;
      
        endforeach; 
}
else {
  _ex('You have no active subscriptions to display.', 'ui', 'memberpress');
}
}

@NUWebStudio
Copy link

I assume you found the solution to the expiry date but incase you havent I found this worked for me:


$user = MeprUtils::get_currentuserinfo();
$subscriptions = $user->active_product_subscriptions('transactions');
    if(!empty($subscriptions)) {
        foreach($subscriptions as $s){
            $expire = $s->expires_at;
            $expire = date('d/m/Y',strtotime($expire));
            echo $expire;
        }
    }
    else {
        _ex('You have no active subscriptions to display.', 'ui', 'memberpress');
    }

@dskurth
Copy link

dskurth commented Nov 25, 2019

I assume you found the solution to the expiry date but incase you havent I found this worked for me:


$user = MeprUtils::get_currentuserinfo();
$subscriptions = $user->active_product_subscriptions('transactions');
    if(!empty($subscriptions)) {
        foreach($subscriptions as $s){
            $expire = $s->expires_at;
            $expire = date('d/m/Y',strtotime($expire));
            echo $expire;
        }
    }
    else {
        _ex('You have no active subscriptions to display.', 'ui', 'memberpress');
    }

Thank you. with a bit of an adjustment of using any user id, perfect results.

@axew3
Copy link

axew3 commented Apr 9, 2021

Would be like this, the best way to associate products ids and related products names? Or what else? Anybody know?

    $user = new MeprUser( $user_id );


    //$active_products = $user->active_product_subscriptions(); // same as ('ids')
    $active_products = $user->active_product_subscriptions('ids');
    $products_titles = $user->get_active_subscription_titles('~'); 
    $products_titles = explode('~',$products_titles);
    $products_ids_titles_assoc_x_user = array_combine($active_products,$products_titles);

That return something like:

Array ( [11000] => Annual Membership - Auto Renewing [13000] => Monthly Membership – Manually Renewing )

@cartpauj
Copy link
Author

cartpauj commented Apr 9, 2021

@axew3

$active_membership_ids = $user->active_product_subscriptions('ids');
$titles = array();
if(!empty($active_membership_ids)) {
  foreach($active_membership_ids as $id) {
    $membership = new MeprProduct($id);
    $titles[] = $membership->post_title;
  }
}

@axew3
Copy link

axew3 commented Apr 9, 2021

@cartpauj Thank you!
I was thinking to check for a different way, since mine was make me feel not secure: not sure if the result is ever correct the way of my snippet (if some order change for example). Anyway your seem to me that do not return the same like into mine, that should be something like:
Array ( [11000] => Annual Membership - Auto Renewing [13000] => Monthly Membership – Manually Renewing )
product ID => product Name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment