I\'m looking for a standard way of getting a user total sum of orders in a date range or for current month.
After exploring woocommerce source code, what I got is, woo i
From your answer here, which I believe is correct.
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;
}
Additional Readings:
DATE FORMATS
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...