【问题标题】:WooCommerce Subscriptions - Get active subscriber info to display in a tableWooCommerce 订阅 - 获取活动订阅者信息以显示在表格中
【发布时间】:2018-02-06 10:25:27
【问题描述】:

我正在使用 Woocommerce 订阅,我想生成一个表格,显示所有活跃订阅者及其相关用户信息。以下代码只是拉起所有用户......关于如何正确执行此操作的任何想法?谢谢! :)

<table style="border-collapse: collapse;" border="0" width="450" cellspacing="0" cellpadding="0"><colgroup> <col span="5" width="75" /> </colgroup>
<tbody>     
<tr>
<td width="75"><strong>Last Name</strong></td>
<td width="75" height="13"><strong>First Name</strong></td>
<td width="75"><strong>Company</strong></td>
<td width="95"><strong>Phone</strong></td>
<td width="75"><strong>Email</strong></td>
</tr>

<?php
$args = array(
    'post_type'   => 'shop_subscription', // Subscription post type
    'post_status' => 'wc-active', // Active subscription
    'order' => 'ASC',
    'meta_key'  => 'last_name',
    'orderby' => 'meta_value', 
    'numberposts' => -1,

);

// The Query

$user_query = new WP_User_Query( $args );


// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {

        echo '
<tr>

<td height="13">' . $user->last_name . '</td> <td>' . $user->first_name . '</td> <td>' . $user->billing_company . '</td><td>' . $user->billing_phone  . '</td> <td>' . $user->user_email  . '</td></tr>' ;
    }
} else {
    echo 'No users found.';
}
?> 


</tbody>
</table>

【问题讨论】:

    标签: php sql wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    您可以通过这种方式使用自定义 SQL 查询来做到这一点(已更新)

    <?php
        global $wpdb;
        $results = $wpdb->get_results( "
            SELECT DISTINCT postmeta.meta_value as user_id, postmeta2.meta_value as first_name, postmeta3.meta_value as last_name,
            postmeta4.meta_value as company, postmeta5.meta_value as phone, postmeta6.meta_value as email
            FROM {$wpdb->prefix}postmeta as postmeta
            INNER JOIN {$wpdb->prefix}posts as posts ON postmeta.post_id = posts.ID
            INNER JOIN {$wpdb->prefix}postmeta as postmeta2 ON postmeta2.post_id = posts.ID
            INNER JOIN {$wpdb->prefix}postmeta as postmeta3 ON postmeta3.post_id = posts.ID
            INNER JOIN {$wpdb->prefix}postmeta as postmeta4 ON postmeta4.post_id = posts.ID
            INNER JOIN {$wpdb->prefix}postmeta as postmeta5 ON postmeta5.post_id = posts.ID
            INNER JOIN {$wpdb->prefix}postmeta as postmeta6 ON postmeta6.post_id = posts.ID
            WHERE posts.post_type LIKE 'shop_subscription'
            AND posts.post_status LIKE 'wc-active'
            AND postmeta.meta_key = '_customer_user'
            AND postmeta2.meta_key = '_billing_first_name'
            AND postmeta3.meta_key = '_billing_last_name'
            AND postmeta4.meta_key = '_billing_company'
            AND postmeta5.meta_key = '_billing_phone'
            AND postmeta6.meta_key = '_billing_email'
            ORDER BY postmeta2.meta_key ASC
        " );
    ?>
    <table style="border-collapse: collapse;" border="0" width="450" cellspacing="0" cellpadding="0">
        <tbody>
            <tr>
                <th height="13"><strong>ID</strong></th>
                <th><strong>Last Name</strong></th>
                <th><strong>First Name</strong></th>
                <th><strong>Company</strong></th>
                <th><strong>Phone</strong></th>
                <th><strong>Email</strong></th>
            </tr>
    <?php
    foreach ( $results as $result ):
    ?>
            <tr>
                <td height="13"><?php echo $result->user_id; ?></td>
                <td><?php echo $result->last_name; ?></td>
                <td><?php echo $result->first_name; ?></td>
                <td><?php echo $result->company; ?></td>
                <td><?php echo $result->phone; ?></td>
                <td><?php echo $result->email; ?></td>
            </tr>
    <?php
    endforeach;
    ?>
        </tbody>
    </table>
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。你会得到这样的东西:

    【讨论】:

    • 太棒了!谢谢!!由于姓氏在名字列中被拉起,我做了一些更改,并且我需要按姓氏字母顺序组织的表格。我将在下面发布更新的代码。
    • 对不起@LoicTheAztec 我是新手,我不确定正确的协议!感谢您的精彩回答!
    猜你喜欢
    • 2022-10-15
    • 2019-07-29
    • 1970-01-01
    • 2019-04-04
    • 2021-10-24
    • 2019-04-26
    • 1970-01-01
    • 2016-12-29
    • 2017-12-03
    相关资源
    最近更新 更多