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

标签 php wordpress woocommerce orders woocommerce-subscriptions

我正在使用 WooCommerce 订阅。

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

Example 01/09/2016 to 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 的所有事件订阅的列表......

此代码已经过测试并且可以工作。

关于php - WooCommerce - 在开始/结束日期之间的列表中获取事件订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39596477/

相关文章:

php - Drupal 7 - 不区分大小写的 LIKE 与 db_select

php - 仅显示 WordPress 子类别

php - 向在新窗口中打开的 Woocommerce 我的帐户订单添加操作按钮

php - 禁用提交按钮停止提交表单 PHP/Javascript

apache - mod_fastcgi 空闲超时不起作用

html - 位于内容下的 Wordpress 侧边栏 - CSS

php - 这里没有参数... PHP 警告 : The argument should be an array. .. 这个错误来自 WP 发布的 PHP 脚本中的哪里?

php - WooCommerce 购物车更改后更新短代码显示

php - 在特定产品的 WooCommerce 购物车页面中的购物车项目名称后添加产品 ID

php - 更改 yii 中的默认页面