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


WooCommerce - Get active subscriptions in a list between start / end date

我正在使用WooCommerce订阅。

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

示例01/09/2016 ~ 15/09/2016

谢谢

下面是一个函数的示例,它将在一个格式化的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>';
}

此代码位于您的活动子主题(或主题)的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);

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