WooCommerce-在开始/结束日期之间的列表中获取有效的订阅 [英] WooCommerce - Get active subscriptions in a list between start / end date

查看:121
本文介绍了WooCommerce-在开始/结束日期之间的列表中获取有效的订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WooCommerce订阅.

I am using WooCommerce subscriptions.

如何从一个特定日期到另一日期获取列表中的所有订阅(包括开始日期和结束日期)?

How can I get all subscriptions (with start date and end date) in a lists from one specific date to another date?

示例01/09/2016至15/09/2016

Example 01/09/2016 to 15/09/2016

谢谢

推荐答案

下面是一个函数示例,该函数将在格式化的html表中显示从一个日期到另一个日期的所有活动订阅的列表.在此示例中,将为每个订阅获取:订阅ID,日期,客户ID和客户名称(您可以自定义代码以获取所需内容).

Here is an example of a function that will display a list of all active subscriptions from one date to another, in a formatted html table. In this example, you will get for each subscription: the subscription ID, the date, the customer ID and the customer name (You can customize the code to get what you want).

因此,此功能有2个日期参数.有关用法和规格,请参阅最后一节.

So this function has 2 date parameters. For usage and specifications see the section at the end.

功能代码:

function active_subscription_list($from_date=null, $to_date=null) {

    // Get all customer orders
    $subscriptions = get_posts( array(
        'numberposts' => -1,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
        'orderby' => 'post_date', // ordered by date
        'order' => 'ASC',
        'date_query' => array( // Start & end date
            array(
                'after'     => $from_date,
                'before'    => $to_date,
                'inclusive' => true,
            ),
        ),
    ) );

    // Styles (temporary, only for demo display) should be removed
    echo "<style>
        .subscription_list th, .subscription_list td{border:solid 1px #666; padding:2px 5px;}
        .subscription_list th{font-weight:bold}
        .subscription_list td{text-align:center}
    </style>";

    // Displaying list in an html table
    echo "<table class='shop_table subscription_list'>
        <tr>
            <th>" . __( 'Number ID', 'your_theme_domain' ) . "</th>
            <th>" . __( 'Date', 'your_theme_domain' ) . "</th>
            <th>" . __( 'User ID', 'your_theme_domain' ) . "</th>
            <th>" . __( 'User Name', 'your_theme_domain' ) . "</th>
        </tr>
            ";
    // Going through each current customer orders
    foreach ( $subscriptions as $subscription ) {
        $subscription_id = $subscription->ID; // subscription ID
        $subscription_date = array_shift( explode( ' ', $subscription->post_date ) ); // Date
        $subscr_meta_data = get_post_meta($subscription->ID);
        $customer_id = $subscr_meta_data['_customer_user'][0]; // customer ID
        $customer_name = $subscr_meta_data['_billing_first_name'][0] . ' ' . $subscr_meta_data['_billing_last_name'][0];
        echo "</tr>
                <td>$subscription_id</td>
                <td>$subscription_date</td>
                <td>$customer_id</td>
                <td>$customer_name</td>
            </tr>";
    }
    echo '</table>';
}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

用法 (示例):

您将必须遵守以下数字日期格式: YEAR-MONTH-DAY

You will have to respect this numerical date format: YEAR-MONTH-DAY

$from_date = '2016-06-19'; // start date
$to_date = '2016-09-21'; // End date

active_subscription_list($from_date, $to_date);

这将显示从2016-06-19到2016-09-21的所有活动订阅的列表...

This will display a list of all active subscriptions from 2016-06-19 to 2016-09-21…

此代码已经过测试并且可以正常工作.

This code is tested and works.

这篇关于WooCommerce-在开始/结束日期之间的列表中获取有效的订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆