Support » Plugin: Membership & Content Restriction - Paid Member Subscriptions » PHP Code to Get List of Users Subscriptions and Subscription Status

  • Resolved aussiedigitalnomad

    (@aussiedigitalnomad)


    Hi,

    I’m trying to get a comma-delimited string of plan ids for the current user, and also the status of a users subscription for a given plan id…
    Here’s the code I’ve got…
    Usage:
    $plan_ids = get_user_subscriptions_details( ‘plans’ );
    or
    $plan_ids = get_user_subscriptions_details( ‘status’, ‘1234’ );

    function get_user_subscriptions_details( $detail_type = null, $plan_id = null ) {

    $user_id = get_current_user_id();
    $subscriptions = pms_get_member_subscriptions( array( ‘user_id’ => $user_id ) );
    $subscription_statuses = pms_get_member_subscription_statuses();

    $ret_value = ”;

    foreach( $subscriptions as $subscription ) {
    if ( is_null( $subscription ) )
    continue;

    if ($detail_type === ‘plans’) {
    // get a comma separated list of all plans the user is subscribed to
    $ret_value .= $subscription->subscription_plan_id . ‘,’;
    }
    elseif ($detail_type === ‘status’) {
    // get the subscription status of the current user
    $ret_value = ‘unknown’;
    if ($subscription->subscription_plan_id === $plan_id) {
    if (!empty( $subscription_statuses[$subscription->status] )) {
    $ret_value = $subscription_statuses[$subscription->status];
    break;
    }
    }
    }
    }
    return $ret_value;
    }`

    It’s bypassing the foreach loop altogether, so the pms_get_member_subscriptions() is not returning required info.
    Any ideas? Thanks in advance

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support dariusraia

    (@dariusraia)

    Hello @aussiedigitalnomad,

    Thank you for reaching out to us.

    pms_get_member_subscriptions() should return an array with existing subscriptions. If you do a var_dump of what you get from pms_get_member_subscriptions, do you receive any info? Perhaps you didn’t map the structure correctly when you processed the array.

    Best regards,

    Plugin Author Georgian Cocora

    (@raster02)

    Hello @aussiedigitalnomad,

    Besides that function, you could also use:

    pms_get_member( $user_id )

    To get a Member object. This will have a key subscriptions with some data about them.

    Regards.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.