• Hello, I am currently developing a project with WordPress and I am using Woocommerce plugin to offer subscriptions to my users. I need help on how to get the quantity of a subscription on PHP.

    I am using this code to get subscription but I can not retrieve quantity:

        $subscriptions = wcs_get_subscriptions( array(
            'customer_id'            => get_current_user_id(),
            'subscription_status'    => 'wc-active',
            'order_by'               => 'DESC',
            'subscriptions_per_page' => - 1
        ) );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The wpdb::query() method returns the number of rows found in any query, after storing the results in wpdb::result. I don’t know how subscriptions are stored, so cannot suggest a proper query. A very generic example:

    global $wpdb;
    $count = $wpdb->query("SELECT user_id FROM subscriptions WHERE status='active'");

    If you examine your DB through phpMyAdmin you might be able to determine the proper query terms to use. If you have trouble doing so, ask the devs of the plugin through their official support channel.

    Thread Starter Besart

    (@besart)

    The subscription is saved as an order in Woocommerce tables and I can retrieve all its data (e.g. start date, end date, purchase date, payment), but quantity is what I’m missing I don’t know how to get it.

    Moderator bcworkz

    (@bcworkz)

    I suspect a count of all orders would not give you the count you are looking for, so the SQL could get a bit complicated because criteria for being counted could also reside in the post meta table. I suggest you ask in the dedicated support forum for WC what an appropriate SQL query would be to get all subscription orders. The devs and expert users there are in the best position to answer this.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to get subscription quantity?’ is closed to new replies.