Skip to content

Commit 99e63a8

Browse files
authored
Merge pull request woocommerce#15054 from woocommerce/improvement/14795
Integrate WC_Order_Query
2 parents 0334ed2 + 9e9b9b8 commit 99e63a8

File tree

5 files changed

+54
-118
lines changed

5 files changed

+54
-118
lines changed

includes/abstracts/abstract-wc-object-query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ protected function get_default_query_vars() {
7171
'parent_exclude' => '',
7272
'exclude' => '',
7373

74-
'limit' => -1,
75-
'page' => '',
74+
'limit' => get_option( 'posts_per_page' ),
75+
'page' => 1,
7676
'offset' => '',
7777
'paginate' => false,
7878

includes/class-wc-order-query.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function get_default_query_vars() {
3737
'cart_tax' => '',
3838
'total' => '',
3939
'total_tax' => '',
40+
'customer' => '',
4041
'customer_id' => '',
4142
'order_key' => '',
4243
'billing_first_name' => '',

includes/data-stores/class-wc-data-store-wp.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,21 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
318318
}
319319

320320
foreach ( $comparisons as $index => $comparison ) {
321-
$query_arg[ $comparison ] = array(
322-
'year' => $dates[ $index ]->date( 'Y' ),
323-
'month' => $dates[ $index ]->date( 'n' ),
324-
'day' => $dates[ $index ]->date( 'j' ),
325-
);
326-
if ( 'second' === $precision ) {
327-
$query_arg[ $comparison ]['minute'] = $dates[ $index ]->date( 'i' );
328-
$query_arg[ $comparison ]['second'] = $dates[ $index ]->date( 's' );
321+
/**
322+
* WordPress doesn't generate the correct SQL for inclusive day queries with both a 'before' and
323+
* 'after' string query, so we have to use the array format in 'day' precision.
324+
* @see https://core.trac.wordpress.org/ticket/29908
325+
*/
326+
if ( 'day' === $precision ) {
327+
$query_arg[ $comparison ]['year'] = $dates[ $index ]->date( 'Y' );
328+
$query_arg[ $comparison ]['month'] = $dates[ $index ]->date( 'n' );
329+
$query_arg[ $comparison ]['day'] = $dates[ $index ]->date( 'j' );
330+
/**
331+
* WordPress doesn't support 'hour'/'second'/'minute' in array format 'before'/'after' queries,
332+
* so we have to use a string query.
333+
*/
334+
} else {
335+
$query_arg[ $comparison ] = gmdate( 'm/d/Y H:i:s', $dates[ $index ]->getTimestamp() );
329336
}
330337
}
331338

@@ -334,11 +341,11 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
334341
$query_arg['month'] = $dates[0]->date( 'n' );
335342
$query_arg['day'] = $dates[0]->date( 'j' );
336343
if ( 'second' === $precision ) {
344+
$query_arg['hour'] = $dates[0]->date( 'H' );
337345
$query_arg['minute'] = $dates[0]->date( 'i' );
338346
$query_arg['second'] = $dates[0]->date( 's' );
339347
}
340348
}
341-
342349
$wp_query_args['date_query'][] = $query_arg;
343350
return $wp_query_args;
344351
}

includes/data-stores/class-wc-order-data-store-cpt.php

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -354,74 +354,14 @@ public function get_order_count( $status ) {
354354
/**
355355
* Get all orders matching the passed in args.
356356
*
357+
* @deprecated 3.1.0 - Use wc_get_orders instead.
357358
* @see wc_get_orders()
358359
* @param array $args
359360
* @return array of orders
360361
*/
361362
public function get_orders( $args = array() ) {
362-
/**
363-
* Generate WP_Query args. This logic will change if orders are moved to
364-
* custom tables in the future.
365-
*/
366-
$wp_query_args = array(
367-
'post_type' => $args['type'] ? $args['type'] : 'shop_order',
368-
'post_status' => $args['status'],
369-
'posts_per_page' => $args['limit'],
370-
'meta_query' => array(),
371-
'fields' => 'ids',
372-
'orderby' => $args['orderby'],
373-
'order' => $args['order'],
374-
);
375-
376-
if ( ! is_null( $args['parent'] ) ) {
377-
$wp_query_args['post_parent'] = absint( $args['parent'] );
378-
}
379-
380-
if ( ! is_null( $args['offset'] ) ) {
381-
$wp_query_args['offset'] = absint( $args['offset'] );
382-
} else {
383-
$wp_query_args['paged'] = absint( $args['page'] );
384-
}
385-
386-
if ( isset( $args['customer'] ) && '' !== $args['customer'] ) {
387-
$values = is_array( $args['customer'] ) ? $args['customer'] : array( $args['customer'] );
388-
$wp_query_args['meta_query'][] = $this->get_orders_generate_customer_meta_query( $values );
389-
}
390-
391-
if ( ! empty( $args['exclude'] ) ) {
392-
$wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] );
393-
}
394-
395-
if ( ! $args['paginate'] ) {
396-
$wp_query_args['no_found_rows'] = true;
397-
}
398-
399-
if ( ! empty( $args['date_before'] ) ) {
400-
$wp_query_args['date_query']['before'] = $args['date_before'];
401-
}
402-
403-
if ( ! empty( $args['date_after'] ) ) {
404-
$wp_query_args['date_query']['after'] = $args['date_after'];
405-
}
406-
407-
// Get results.
408-
$orders = new WP_Query( apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $args, $this ) );
409-
410-
if ( 'objects' === $args['return'] ) {
411-
$return = array_map( 'wc_get_order', $orders->posts );
412-
} else {
413-
$return = $orders->posts;
414-
}
415-
416-
if ( $args['paginate'] ) {
417-
return (object) array(
418-
'orders' => $return,
419-
'total' => $orders->found_posts,
420-
'max_num_pages' => $orders->max_num_pages,
421-
);
422-
} else {
423-
return $return;
424-
}
363+
wc_deprecated_function( 'WC_Order_Data_Store_CPT::get_orders', '3.1.0', 'Use wc_get_orders instead.' );
364+
return wc_get_orders( $args );
425365
}
426366

427367
/**
@@ -684,11 +624,16 @@ protected function get_wp_query_args( $query_vars ) {
684624
}
685625
}
686626

627+
if ( isset( $query_vars['customer'] ) && '' !== $query_vars['customer'] && array() !== $query_vars['customer'] ) {
628+
$values = is_array( $query_vars['customer'] ) ? $query_vars['customer'] : array( $query_vars['customer'] );
629+
$wp_query_args['meta_query'][] = $this->get_orders_generate_customer_meta_query( $values );
630+
}
631+
687632
if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) {
688633
$wp_query_args['no_found_rows'] = true;
689634
}
690635

691-
return apply_filters( 'woocommerce_get_order_wp_query_args', $wp_query_args, $query_vars );
636+
return apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $query_vars, $this );
692637
}
693638

694639
/**

includes/wc-order-functions.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,26 @@
1313
}
1414

1515
/**
16-
* Wrapper for get_posts specific to orders.
16+
* Standard way of retrieving orders based on certain parameters.
1717
*
1818
* This function should be used for order retrieval so that when we move to
1919
* custom tables, functions still work.
2020
*
21-
* Args:
22-
* status array|string List of order statuses to find
23-
* type array|string Order type, e.g. shop_order or shop_order_refund
24-
* parent int post/order parent
25-
* customer int|string|array User ID or billing email to limit orders to a
26-
* particular user. Accepts array of values. Array of values is OR'ed. If array of array is passed, each array will be AND'ed.
27-
* e.g. [email protected], 1, array( 1, 2, 3 ), array( array( 1, '[email protected]' ), 2, 3 )
28-
* limit int Maximum of orders to retrieve.
29-
* offset int Offset of orders to retrieve.
30-
* page int Page of orders to retrieve. Ignored when using the 'offset' arg.
31-
* date_before string Get orders before a certain date ( strtotime() compatibile string )
32-
* date_after string Get orders after a certain date ( strtotime() compatibile string )
33-
* exclude array Order IDs to exclude from the query.
34-
* orderby string Order by date, title, id, modified, rand etc
35-
* order string ASC or DESC
36-
* return string Type of data to return. Allowed values:
37-
* ids array of order ids
38-
* objects array of order objects (default)
39-
* paginate bool If true, the return value will be an array with values:
40-
* 'orders' => array of data (return value above),
41-
* 'total' => total number of orders matching the query
42-
* 'max_num_pages' => max number of pages found
21+
* Args and usage: @todo link to wiki page.
4322
*
4423
* @since 2.6.0
4524
* @param array $args Array of args (above)
4625
* @return array|stdClass Number of pages and an array of order objects if
4726
* paginate is true, or just an array of values.
4827
*/
4928
function wc_get_orders( $args ) {
50-
$args = wp_parse_args( $args, array(
51-
'status' => array_keys( wc_get_order_statuses() ),
52-
'type' => wc_get_order_types( 'view-orders' ),
53-
'parent' => null,
54-
'customer' => null,
55-
'email' => '',
56-
'limit' => get_option( 'posts_per_page' ),
57-
'offset' => null,
58-
'page' => 1,
59-
'exclude' => array(),
60-
'orderby' => 'date',
61-
'order' => 'DESC',
62-
'return' => 'objects',
63-
'paginate' => false,
64-
'date_before' => '',
65-
'date_after' => '',
66-
) );
67-
68-
// Handle some BW compatibility arg names where wp_query args differ in naming.
6929
$map_legacy = array(
7030
'numberposts' => 'limit',
7131
'post_type' => 'type',
7232
'post_status' => 'status',
7333
'post_parent' => 'parent',
7434
'author' => 'customer',
35+
'email' => 'billing_email',
7536
'posts_per_page' => 'limit',
7637
'paged' => 'page',
7738
);
@@ -82,7 +43,29 @@ function wc_get_orders( $args ) {
8243
}
8344
}
8445

85-
return WC_Data_Store::load( 'order' )->get_orders( $args );
46+
// Map legacy date args to modern date args.
47+
$date_before = false;
48+
$date_after = false;
49+
50+
if ( ! empty( $args['date_before'] ) ) {
51+
$datetime = wc_string_to_datetime( $args['date_before'] );
52+
$date_before = strpos( $args['date_before'], ':' ) ? $datetime->getOffsetTimestamp() : $datetime->date( 'Y-m-d' );
53+
}
54+
if ( ! empty( $args['date_after'] ) ) {
55+
$datetime = wc_string_to_datetime( $args['date_after'] );
56+
$date_after = strpos( $args['date_after'], ':' ) ? $datetime->getOffsetTimestamp() : $datetime->date( 'Y-m-d' );
57+
}
58+
59+
if ( $date_before && $date_after ) {
60+
$args['date_created'] = $date_before . '...' . $date_after;
61+
} elseif ( $date_before ) {
62+
$args['date_created'] = '<' . $date_before;
63+
} elseif ( $date_after ) {
64+
$args['date_created'] = '>' . $date_after;
65+
}
66+
67+
$query = new WC_Order_Query( $args );
68+
return $query->get_orders();
8669
}
8770

8871
/**

0 commit comments

Comments
 (0)