How to get user orders by date in woocommerce?

Mehran

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 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
        ) );

in class-wc-report-sales-by-product.php

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.

Thanks

Reigel

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get custom fields values from WooCommerce orders

From Dev

Get list of woocommerce orders plugin

From Dev

WooCommerce widget - List of orders from date to date

From Dev

Woocommerce change role by total of user orders

From Dev

Get sku from each orders Woocommerce

From Dev

How to get purchase date from woocommerce order?

From Dev

How to get purchase date from woocommerce order?

From Dev

How does WooCommerce get a user's country

From Dev

How To add column on Woocommerce -> Orders backend page

From Dev

Woocommerce - how to match orders and items back to customers

From Dev

How to isolate(seperate) Orders section in Wordpress WooCommerce?

From Dev

Get orders of a user with orderItems and product details in Laravel

From Dev

How to use a Date Field for Yesterday orders and YTD orders for customers?

From Dev

How to get all not answered orders

From Dev

WooCommerce - Getting completed status orders by user in a period of time

From Dev

Display custom column with custom user meta in woocommerce orders page

From Dev

Display custom tax total row based on user roles in WooCommerce orders

From Dev

How to reverse number orders in Excel to formate a date?

From Dev

How to get User creation date in Orchard?

From Dev

How to get the date from user input in Java?

From Dev

How to get User creation date in Orchard?

From Dev

“Get product X for free on orders over $100” coupon code in WooCommerce

From Dev

Get used coupon codes and related discount amounts from WooCommerce orders

From Dev

Get used coupon codes and related discount amounts from WooCommerce orders

From Java

how i can retrieve all woocommerce orders in new custom plugin

From Dev

how to delete completed orders in woocommerce using a my sql query

From Dev

how limit the number of orders by customer at time range in WooCommerce

From Dev

How to show a product custom field (custom SKU) in WooCommerce orders

From Dev

How to change Woocommerce shipping labels in cart, checkout and also on orders

Related Related

  1. 1

    How to get custom fields values from WooCommerce orders

  2. 2

    Get list of woocommerce orders plugin

  3. 3

    WooCommerce widget - List of orders from date to date

  4. 4

    Woocommerce change role by total of user orders

  5. 5

    Get sku from each orders Woocommerce

  6. 6

    How to get purchase date from woocommerce order?

  7. 7

    How to get purchase date from woocommerce order?

  8. 8

    How does WooCommerce get a user's country

  9. 9

    How To add column on Woocommerce -> Orders backend page

  10. 10

    Woocommerce - how to match orders and items back to customers

  11. 11

    How to isolate(seperate) Orders section in Wordpress WooCommerce?

  12. 12

    Get orders of a user with orderItems and product details in Laravel

  13. 13

    How to use a Date Field for Yesterday orders and YTD orders for customers?

  14. 14

    How to get all not answered orders

  15. 15

    WooCommerce - Getting completed status orders by user in a period of time

  16. 16

    Display custom column with custom user meta in woocommerce orders page

  17. 17

    Display custom tax total row based on user roles in WooCommerce orders

  18. 18

    How to reverse number orders in Excel to formate a date?

  19. 19

    How to get User creation date in Orchard?

  20. 20

    How to get the date from user input in Java?

  21. 21

    How to get User creation date in Orchard?

  22. 22

    “Get product X for free on orders over $100” coupon code in WooCommerce

  23. 23

    Get used coupon codes and related discount amounts from WooCommerce orders

  24. 24

    Get used coupon codes and related discount amounts from WooCommerce orders

  25. 25

    how i can retrieve all woocommerce orders in new custom plugin

  26. 26

    how to delete completed orders in woocommerce using a my sql query

  27. 27

    how limit the number of orders by customer at time range in WooCommerce

  28. 28

    How to show a product custom field (custom SKU) in WooCommerce orders

  29. 29

    How to change Woocommerce shipping labels in cart, checkout and also on orders

HotTag

Archive