如何在woocommerce中按日期获取用户订单? [英] How to get user orders by date in woocommerce?
问题描述
我正在寻找一种标准的方法来获取用户在日期范围内或当月的订单总数.
I'm looking for a standard way of getting a user total sum of orders in a date range or for current month.
浏览woocommerce源代码后,我得到的是,woo正在使用类似的东西
After exploring woocommerce source code, what I got is, woo is using something like this
$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
) );
在wc-report-by-product.php类中
in class-wc-report-sales-by-product.php
但是,正如您所知,这是基于产品而不是用户的.
从上面的代码,
$this->group_by_query
部分保存可以在woo源中找到的日期的条件.我的问题实际上是关于如何使用woocommerce的内置功能根据给定的日期范围生成订单列表.
But as you know, this works based on products not users.
from the above code,
$this->group_by_query
part holds the conditions for dates which can be found in woo source. My question is actually about how to use a builtin function of woocommerce to generate a list of orders based on given date range.
谢谢
推荐答案
From your answer here, which I believe is correct.
您只需要在字段中添加 date_query
...像这样:
You just need to add the date_query
in the fields... something like this:
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;
}
其他读物:
插入有关如何使用get_order_report_data
的问题,您可以通过这种方式进行操作...您可以将其粘贴到主题中的functions.php
中进行测试.
inline with the question on how to use get_order_report_data
, you can do it this way... you can paste this in your functions.php
on the theme to test.
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 )
请注意上面代码中的注释...
Please take note of the comments in the codes above...
这篇关于如何在woocommerce中按日期获取用户订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!