Get Woocommerce Subscription Start Date

I realize this may full well be a total novice question but at this point I’m so frustrated I don’t even care. I’m trying to create a variable with the current users subscription start date – if they have one – and I’m stuck at pulling in the start date.

This is what I have so far – it won’t be a shortcode in the end (it will be a variable I put into a function) I’ve just got it set up as a shortcode so I can see the output – and it returns the whole array of the users stored info. I’m just after the start date 🙂

Read More
function subscriber_start_date(){
    $start_date = WC_Subscriptions_Manager::get_users_subscription( $user_id, 'start_date' );
    print_r($start_date);
}

add_shortcode("subscriber-start-date","subscriber_start_date");

I’ve looked at the documents here and here but am still just drawing up nothing on this and I know it’s going to be something stupid simple like adding a variable somewhere, a comma, or [”] – I’ve tried a myriad of combinations of things that make sense to me but nothing is working (this is the only combination that’s returned anything of use).

Any HELPFUL comments would be greatly appreciated. Thanks in advance.

Related posts

1 comment

  1. It’s worth mentioning a few things:

    First, this isn’t Core WooCommerce, it’s an add-on (Subscriptions).

    Second, the documentation you linked to says specifically:

    …each subscription is returned as an array with the following values…

    That means the function isn’t going to return just the start date.

    Put another way, the second field is a subscription identifier. A user can have more than one subscription. This function allows you to get a specific subscription, not a specific field from the subscription.

    So, in order to do what you want, you need to add to your code as follows:

    function subscriber_start_date() {
        // NOTE: You don't have the $user_id - are you setting it?
    
        // Somehow you need to identify the subscription you want.
        $subscription_id = 'MY_SUBSCRIPTION_ID';
        $subscription = WC_Subscriptions_Manager::get_users_subscription( $user_id, $subscription_id );
    
        $start_date = (isset($subscription['start_date'])) ? $subscription['start_date'] : FALSE;
    
        var_dump($start_date);
    }
    
    add_shortcode("subscriber-start-date","subscriber_start_date");
    

    If you do NOT know the subscription ID, then you could do something like this to get the “first” subscription for the user:

    function subscriber_start_date() {
        // Set start date to initial value
        $start_date = FALSE;
        // Get ALL subscriptions
        $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions( $user_id );
        if ($subscriptions) {
            // Get the first subscription
            $subscription = array_shift($subscriptions);
            // Get the start date, if set
            $start_date = (isset($subscription['start_date'])) ? $subscription['start_date'] : FALSE;
        }
    
        return $start_date;
    }
    

Comments are closed.