0

What i would like to do is have a list with all the customers on my website who has an active subscription with the WooCommerce Subscriptions plugin.

I would really appreciate your help with this. I have searched the internet without finding a solution.

I have tried to find a way in Woocommerce Subscriptions Developer Docs without success. I have tried creating different shortcodes but none seem to be working. Here is one example found here: Get active members list for a subscription plan in Woocommerce subscriptions (this returns an empty list with only the headings):

// The SQL query
function get_active_subscribers_for_plan( $product_id = 71164, $variation_id = 0 ) {
    global $wpdb;

    if( $variation_id > 0 ){
        $query_subscription_plan = "AND woim.meta_key = '_variation_id' AND woim.meta_value = '$variation_id'";
    } else {
        $query_subscription_plan = "AND woim.meta_key = '_product_id' AND woim.meta_value = '$product_id'";
    }

    $results = $wpdb->get_results( "
        SELECT DISTINCT pm1.meta_value as user_id, pm2.meta_value as first_name,
        pm3.meta_value as last_name, pm4.meta_value as phone, pm5.meta_value as email
        FROM {$wpdb->prefix}posts as p
        JOIN {$wpdb->prefix}postmeta as pm1 ON p.ID = pm1.post_id
        JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
        JOIN {$wpdb->prefix}postmeta as pm3 ON p.ID = pm3.post_id
        JOIN {$wpdb->prefix}postmeta as pm4 ON p.ID = pm4.post_id
        JOIN {$wpdb->prefix}postmeta as pm5 ON p.ID = pm5.post_id
        JOIN {$wpdb->prefix}posts as p2 ON p.ID = p2.post_parent
        JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p2.ID = woi.order_id
        JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_type = 'shop_order'
        AND p2.post_type = 'shop_subscription'
        AND p2.post_status = 'wc-active'
        AND pm1.meta_key = '_customer_user'
        AND pm2.meta_key = '_billing_first_name'
        AND pm3.meta_key = '_billing_last_name'
        AND pm4.meta_key = '_billing_phone'
        AND pm5.meta_key = '_billing_email'
        $query_subscription_plan
    ");

    return $results;
}

// The Html Output
function display_active_subscribers_for_plan( $product_id = 0, $variation_id = 0 ){
    // The query
    $results = get_active_subscribers_for_plan( $product_id, $variation_id );

    // The display
    echo '<table>
    <thead><tr>
        <th>' . __("User ID","woocommerce") . '</th>
        <th>' . __("First name","woocommerce") . '</th>
        <th>' . __("Last name","woocommerce") . '</th>
        <th>' . __("Phone","woocommerce") . '</th>
        <th>' . __("Email","woocommerce") . '</th>
    </tr></thead>
    <tbody>';
    // Loop through each subscriber
    foreach( $results as $result ){
        // Edit User Url
        $edit_user_url = home_url('/wp-admin/user-edit.php?user_id=' . $result->user_id . '/');

        echo '<tr>
        <td><a href="' . $edit_user_url . '">' . $result->user_id . '</a></td>
        <td>' . $result->first_name . '</td>
        <td>' . $result->last_name . '</td>
        <td>' . $result->phone . '</td>
        <td><a href="malto:' . $result->email . '">' . $result->email . '</a></td>
        </tr>';
    }
    echo '</tbody></table>';
}

The problem with this is that it lists no subscribers.

I appreciate your help.

Thank you.

I tried this solution: Get active members list for a subscription plan in Woocommerce subscriptions

But i can not get it to work. Maybe i am calling the function incorrectly. Can someone please tell me how to call this in elementor. I am not that good in this, maybe this is basic but i cannot figure it out.

  • Check this answer - https://stackoverflow.com/questions/55577445/how-to-get-list-of-active-subscribers-in-woocommerce – Snuffy Jan 13 '23 at 07:15

0 Answers0