【问题标题】:Get active members list for a subscription plan in Woocommerce subscriptions [closed]在 Woocommerce 订阅中获取订阅计划的活跃成员列表 [关闭]
【发布时间】:2019-04-26 15:17:03
【问题描述】:

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

我想获得他们的名字、姓氏和计费电话,以便我的员工可以轻松了解他们的活动状态。

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

感谢任何帮助。

【问题讨论】:

    标签: php sql wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    这可以使用自定义 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' );
    

    你会得到类似的东西:

    【讨论】:

    • 谢谢,但如果我想添加自定义元字段怎么办?
    • 喜欢地址和开始和结束日期
    • 好的,我会测试这个功能并回复你,所以你能确认它只有“活跃”成员吗?
    • @GamalElwazeery 我已添加此行AND p2.post_status = 'wc-active',它将仅查询有效订阅。
    猜你喜欢
    • 2020-11-02
    • 2017-08-05
    • 2016-12-29
    • 2018-02-06
    • 2014-05-24
    • 1970-01-01
    • 2017-12-03
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多