Search code examples
wordpresswoocommercewoocommerce-memberships

Output Subscription Number in WooCommerce to use as a Membership Number


I am using WooCommerce with Memberships and Subscriptions. There's two parts to my question. Firstly I am not sure what the best practice is when we want to assign a membership number to a member. A the moment the memberships are assigned to a subscription product so it makes sense to use the subscription product as a member ID. I don't think there's any other option. To that end, I want to output this subscription number as a member ID. There will not be any other subscription products. So the second part of my question is how to print the membership number, something like this:

    $user_id = $current_user->ID;
    $subs = wcs_get_users_subscriptions($user_id)
    <em class="mem-num"><?php echo '#'.$subs;?></em>

This produces #Array

So in actual fact, I probably need to iterate through an array that will only have one value and return it as the ID.


Solution

  • I managed to put together a solution through this answer here: WooCommerce Subscriptions - Get related orders Ids for a specific subscription.

    $customer_subscriptions = get_posts( array(
            'numberposts' => -1,
    
            'post_type'   => 'shop_subscription', 
            'post_status' => 'wc-active' 
        ) );
    
                foreach( $customer_subscriptions as $customer_subscription ){
            // The subscription ID
            $subscription_id = $customer_subscription->ID;
    
                    wc_get_order( $subscription_id );
    
                }
    
    <div class="mem-num"><?php echo '<i class="fas fa-user"></i> Your Membership Number: '.$subscription_id;?></div>
    

    I didn't want to get the order number from the subscription ID just to output the subscription ID as the membership number.