【问题标题】:How to get user orders by date in woocommerce?如何在 woocommerce 中按日期获取用户订单?
【发布时间】:2016-05-10 20:33:09
【问题描述】:

我正在寻找一种标准方法来获取用户在某个日期范围内或当月的订单总数。

在探索了 woocommerce 源代码之后,我得到的是,woo 正在使用类似这样的东西

            $order_item_amounts = $this->get_order_report_data( array(
            'data' => array(
                '_line_total' => array(
                    'type'            => 'order_item_meta',
                    'order_item_type' => 'line_item',
                    'function' => 'SUM',
                    'name'     => 'order_item_amount'
                ),
                'post_date' => array(
                    'type'     => 'post_data',
                    'function' => '',
                    'name'     => 'post_date'
                ),
                '_product_id' => array(
                    'type'            => 'order_item_meta',
                    'order_item_type' => 'line_item',
                    'function'        => '',
                    'name'            => 'product_id'
                ),
            ),
            'where_meta' => array(
                'relation' => 'OR',
                array(
                    'type'       => 'order_item_meta',
                    'meta_key'   => array( '_product_id', '_variation_id' ),
                    'meta_value' => $this->product_ids,
                    'operator'   => 'IN'
                ),
            ),
            'group_by'     => 'product_id, ' . $this->group_by_query,
            'order_by'     => 'post_date ASC',
            'query_type'   => 'get_results',
            'filter_range' => true
        ) );

在 class-wc-report-sales-by-product.php 中

但如您所知,这是基于产品而非用户的。 从上面的代码, $this->group_by_query 部分包含可在 woo 源中找到的日期条件。我的问题实际上是关于如何使用 woocommerce 的内置函数根据给定的日期范围生成订单列表。

谢谢

【问题讨论】:

    标签: php wordpress plugins woocommerce


    【解决方案1】:

    From your answer here,我认为是正确的。

    您只需要在字段中添加date_query...类似这样:

    public function get_customer_total_order() {
        $customer_orders = get_posts( array(
            'numberposts' => - 1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => array( 'shop_order' ),
            'post_status' => array( 'wc-completed' ),
            'date_query' => array(
                'after' => date('Y-m-d', strtotime('-10 days')),
                'before' => date('Y-m-d', strtotime('today')) 
            )
    
        ) );
    
        $total = 0;
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order );
            $total += $order->get_total();
        }
    
        return $total;
    }
    

    补充阅读:

    DATE FORMATS

    与如何使用get_order_report_data 的问题一致,您可以这样做...您可以将其粘贴到主题上的functions.php 中进行测试。

    include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php');
    
    $reports = new WC_Admin_Report();
    $args = array(
        'data' => array(
            '_order_total' => array(
                'type'     => 'meta',
                'function' => 'SUM',
                'name'     => 'total_sales'
            ),
        ),
        'where' => array(
            array(
                'key'      => 'post_date',
                'value'    => date( 'Y-m-d', strtotime( '01/01/2016' ) ), // starting date
                'operator' => '>'
            ),
            array(
                'key'      => 'post_date',
                'value'    => date( 'Y-m-d', strtotime( '02/01/2016' ) ), // end date...
                'operator' => '<'
            ),
        ),
        'where_meta' => array(
            array(
                'meta_key'   => '_customer_user',
                'meta_value' => '1', // customer id
                'operator'   => '='
            )
        ),
    );
    $data = $reports->get_order_report_data($args);
    print_r($data); // prints like: stdClass Object ( [total_sales] => 200 )
    

    请注意上面代码中的cmets...

    【讨论】:

    • 感谢您的回答,正确。实际上,当我记得 Wordpress date_query 时,我在几分钟前实施了这种方法。这是一个好方法。但是是否有任何其他方法只通过一个查询来完成所有操作,例如 get_order_report_data()
    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 2016-10-23
    • 2020-08-17
    • 2021-07-31
    • 1970-01-01
    • 2019-07-08
    • 2016-12-16
    • 2015-09-07
    相关资源
    最近更新 更多