0

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

我想知道他们的名字、姓氏和计费电话,这样我的员工就可以轻松地知道他们是如何工作的。

我试图在Woocommerce Subscriptions Developer Docs中找到一种方法,但没有成功。

任何帮助表示赞赏。

4

1 回答 1

2

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

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

第二个函数将在 html 表中显示结果,其中包含用户 ID、计费名字、计费姓氏、计费电话和计费电子邮件。

编码:

// 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 (产品ID在哪里91

display_active_subscribers_for_plan( 91 );

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

display_active_subscribers_for_plan( 149, 'variation' );

你会得到类似的东西:

在此处输入图像描述

于 2018-11-24T15:18:11.747 回答