5

Context:

Given a WordPress website with WooCommerce and WooCommerce Subscriptions, I am attempting to fetch a list of a specific user's subscriptions. Until the recent update, this one line of code has taken care of this for me. Here's is the code that I've been using:

$all_user_subscriptions = WC_Subscriptions_Manager::get_users_subscriptions( $user_id );

Where $user_id is a valid user's ID in WordPress.

The Problem:

We are seeing the following error frequently since the last update:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Start or end times are not integers' in /home/warfarep/public_html/wp-content/plugins/woocommerce-subscriptions/includes/wcs-time-functions.php:332 Stack trace: #0 /home/warfarep/public_html/wp-content/plugins/woocommerce-subscriptions/includes/wcs-time-functions.php(309): wcs_number_of_leap_days('1456308046', 1487930566) #1 /home/warfarep/public_html/wp-content/plugins/woocommerce-subscriptions/includes/wcs-deprecated-functions.php(171): wcs_estimate_periods_between('1456308046', 1487930566, 'year', 'floor') #2 /home/warfarep/public_html/wp-content/plugins/woocommerce-subscriptions/includes/class-wc-subscriptions-manager.php(1460): wcs_get_subscription_in_deprecated_structure(Object(WC_Subscription)) #3 /home/warfarep/public_html/wp-content/themes/warfare-plugins-pro-3/functions.php(334): WC_Subscriptions_Manager::get_users_subscriptions('2686') #4 /home/warfarep/public_html/wp-content/themes/warfare-plugins-pro-3/woocommerce/myaccount/my-account.ph in /home/warfarep/public_html/wp-content/plugins/woocommerce-subscriptions/includes/wcs-time-functions.php on line 332

The Question:

If this function no longer works (is deprecated) what can I do to retrieve a list (object, array, whatever) of a specific user's subscriptions without throwing a fatal error?

Nicholas Cardot
  • 956
  • 1
  • 11
  • 30

2 Answers2

13

I believe you can now use the wcs_get_users_subscriptions($user_id) function to get the Subscriptions object.

0

You can find the array of active subscription ids by using this WC_Subscriptions_Order::get_users_subscription_orders( $user_id );

To get a more specific subscription

wcs_get_subscriptions( [
            'subscriptions_per_page' => 10,
            'paged'                  => 1,
            'offset'                 => 0,
            'orderby'                => 'start_date',
            'order'                  => 'DESC',
            'customer_id'            => 0,
            'product_id'             => 0,
            'variation_id'           => 0,
            'order_id'               => 0,
            'subscription_status'    => array( 'any' ),
            'meta_query_relation'    => 'AND',
] );
Rafiq
  • 86
  • 1
  • 8