在 Woocommerce 订阅中获取订阅计划的活跃成员列表

Get active members list for a subscription plan in Woocommerce subscriptions

我正在尝试使用 Woocommerce 订阅来检索和显示特定订阅计划的活跃成员。

我想知道他们的名字、姓氏和帐单 phone,这样我的员工就可以轻松地知道他们是否仍然活跃。

我曾尝试在 Woocommerce Subscriptions Developer Docs 中寻找方法,但没有成功。

感谢任何帮助。

这可以使用自定义 SQL 查询来完成。

下面的第一个函数将根据作为目标订阅计划的产品 ID(或变体 ID)进行查询。

第二个函数将在 html table 中显示结果,其中包含用户 ID、账单名字、账单姓氏、账单 phone 和账单电子邮件.

代码:

// The SQL query
function get_active_subscribers_for_plan( $product_id = 0, $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>';
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。


用法

1) 对于简单的订阅类型和定义的产品 ID (其中 91 是产品 ID)

display_active_subscribers_for_plan( 91 );

2) 对于变体订阅类型和定义的变体 ID (其中 149 是产品 ID)

display_active_subscribers_for_plan( 149, 'variation' );

你会得到类似的东西: