WooCommerce - 在开始/结束日期之间的列表中获取有效订阅

Leo*_*eon 3 php wordpress orders woocommerce woocommerce-subscriptions

我正在使用WooCommerce订阅.

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

示例01/09/2016至2016年9月15日

谢谢

Loi*_*tec 12

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

所以这个函数有2个日期参数.有关用法和规格,请参阅最后一节.

功能代码:

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>';
}
Run Code Online (Sandbox Code Playgroud)

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.


用法 (例子):

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

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

active_subscription_list($from_date, $to_date);
Run Code Online (Sandbox Code Playgroud)

这将显示2016-06-19至2016-09-21所有有效订阅的列表...

此代码经过测试和运行.