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

时间:2018-11-24 08:09:46

标签: php sql wordpress woocommerce woocommerce-subscriptions

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

我想知道他们的名字,姓氏和帐单电话,以便我的员工可以轻松地知道如何保持活跃。

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

感谢您的帮助。

1 个答案:

答案 0 :(得分: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 (其中91是产品ID)

display_active_subscribers_for_plan( 91 );

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

display_active_subscribers_for_plan( 149, 'variation' );

您将得到类似的东西:

enter image description here