Merge pull request #15054 from woocommerce/improvement/14795

Integrate WC_Order_Query
This commit is contained in:
Mike Jolley 2017-05-15 10:24:47 +01:00 committed by GitHub
commit 99e63a8ca9
5 changed files with 54 additions and 118 deletions

View File

@ -71,8 +71,8 @@ abstract class WC_Object_Query {
'parent_exclude' => '',
'exclude' => '',
'limit' => -1,
'page' => '',
'limit' => get_option( 'posts_per_page' ),
'page' => 1,
'offset' => '',
'paginate' => false,

View File

@ -37,6 +37,7 @@ class WC_Order_Query extends WC_Object_Query {
'cart_tax' => '',
'total' => '',
'total_tax' => '',
'customer' => '',
'customer_id' => '',
'order_key' => '',
'billing_first_name' => '',

View File

@ -318,14 +318,21 @@ class WC_Data_Store_WP {
}
foreach ( $comparisons as $index => $comparison ) {
$query_arg[ $comparison ] = array(
'year' => $dates[ $index ]->date( 'Y' ),
'month' => $dates[ $index ]->date( 'n' ),
'day' => $dates[ $index ]->date( 'j' ),
);
if ( 'second' === $precision ) {
$query_arg[ $comparison ]['minute'] = $dates[ $index ]->date( 'i' );
$query_arg[ $comparison ]['second'] = $dates[ $index ]->date( 's' );
/**
* WordPress doesn't generate the correct SQL for inclusive day queries with both a 'before' and
* 'after' string query, so we have to use the array format in 'day' precision.
* @see https://core.trac.wordpress.org/ticket/29908
*/
if ( 'day' === $precision ) {
$query_arg[ $comparison ]['year'] = $dates[ $index ]->date( 'Y' );
$query_arg[ $comparison ]['month'] = $dates[ $index ]->date( 'n' );
$query_arg[ $comparison ]['day'] = $dates[ $index ]->date( 'j' );
/**
* WordPress doesn't support 'hour'/'second'/'minute' in array format 'before'/'after' queries,
* so we have to use a string query.
*/
} else {
$query_arg[ $comparison ] = gmdate( 'm/d/Y H:i:s', $dates[ $index ]->getTimestamp() );
}
}
@ -334,11 +341,11 @@ class WC_Data_Store_WP {
$query_arg['month'] = $dates[0]->date( 'n' );
$query_arg['day'] = $dates[0]->date( 'j' );
if ( 'second' === $precision ) {
$query_arg['hour'] = $dates[0]->date( 'H' );
$query_arg['minute'] = $dates[0]->date( 'i' );
$query_arg['second'] = $dates[0]->date( 's' );
}
}
$wp_query_args['date_query'][] = $query_arg;
return $wp_query_args;
}

View File

@ -354,74 +354,14 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
/**
* Get all orders matching the passed in args.
*
* @deprecated 3.1.0 - Use wc_get_orders instead.
* @see wc_get_orders()
* @param array $args
* @return array of orders
*/
public function get_orders( $args = array() ) {
/**
* Generate WP_Query args. This logic will change if orders are moved to
* custom tables in the future.
*/
$wp_query_args = array(
'post_type' => $args['type'] ? $args['type'] : 'shop_order',
'post_status' => $args['status'],
'posts_per_page' => $args['limit'],
'meta_query' => array(),
'fields' => 'ids',
'orderby' => $args['orderby'],
'order' => $args['order'],
);
if ( ! is_null( $args['parent'] ) ) {
$wp_query_args['post_parent'] = absint( $args['parent'] );
}
if ( ! is_null( $args['offset'] ) ) {
$wp_query_args['offset'] = absint( $args['offset'] );
} else {
$wp_query_args['paged'] = absint( $args['page'] );
}
if ( isset( $args['customer'] ) && '' !== $args['customer'] ) {
$values = is_array( $args['customer'] ) ? $args['customer'] : array( $args['customer'] );
$wp_query_args['meta_query'][] = $this->get_orders_generate_customer_meta_query( $values );
}
if ( ! empty( $args['exclude'] ) ) {
$wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] );
}
if ( ! $args['paginate'] ) {
$wp_query_args['no_found_rows'] = true;
}
if ( ! empty( $args['date_before'] ) ) {
$wp_query_args['date_query']['before'] = $args['date_before'];
}
if ( ! empty( $args['date_after'] ) ) {
$wp_query_args['date_query']['after'] = $args['date_after'];
}
// Get results.
$orders = new WP_Query( apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $args, $this ) );
if ( 'objects' === $args['return'] ) {
$return = array_map( 'wc_get_order', $orders->posts );
} else {
$return = $orders->posts;
}
if ( $args['paginate'] ) {
return (object) array(
'orders' => $return,
'total' => $orders->found_posts,
'max_num_pages' => $orders->max_num_pages,
);
} else {
return $return;
}
wc_deprecated_function( 'WC_Order_Data_Store_CPT::get_orders', '3.1.0', 'Use wc_get_orders instead.' );
return wc_get_orders( $args );
}
/**
@ -684,11 +624,16 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
}
}
if ( isset( $query_vars['customer'] ) && '' !== $query_vars['customer'] && array() !== $query_vars['customer'] ) {
$values = is_array( $query_vars['customer'] ) ? $query_vars['customer'] : array( $query_vars['customer'] );
$wp_query_args['meta_query'][] = $this->get_orders_generate_customer_meta_query( $values );
}
if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) {
$wp_query_args['no_found_rows'] = true;
}
return apply_filters( 'woocommerce_get_order_wp_query_args', $wp_query_args, $query_vars );
return apply_filters( 'woocommerce_order_data_store_cpt_get_orders_query', $wp_query_args, $query_vars, $this );
}
/**

View File

@ -13,33 +13,12 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Wrapper for get_posts specific to orders.
* Standard way of retrieving orders based on certain parameters.
*
* This function should be used for order retrieval so that when we move to
* custom tables, functions still work.
*
* Args:
* status array|string List of order statuses to find
* type array|string Order type, e.g. shop_order or shop_order_refund
* parent int post/order parent
* customer int|string|array User ID or billing email to limit orders to a
* particular user. Accepts array of values. Array of values is OR'ed. If array of array is passed, each array will be AND'ed.
* e.g. test@test.com, 1, array( 1, 2, 3 ), array( array( 1, 'test@test.com' ), 2, 3 )
* limit int Maximum of orders to retrieve.
* offset int Offset of orders to retrieve.
* page int Page of orders to retrieve. Ignored when using the 'offset' arg.
* date_before string Get orders before a certain date ( strtotime() compatibile string )
* date_after string Get orders after a certain date ( strtotime() compatibile string )
* exclude array Order IDs to exclude from the query.
* orderby string Order by date, title, id, modified, rand etc
* order string ASC or DESC
* return string Type of data to return. Allowed values:
* ids array of order ids
* objects array of order objects (default)
* paginate bool If true, the return value will be an array with values:
* 'orders' => array of data (return value above),
* 'total' => total number of orders matching the query
* 'max_num_pages' => max number of pages found
* Args and usage: @todo link to wiki page.
*
* @since 2.6.0
* @param array $args Array of args (above)
@ -47,31 +26,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* paginate is true, or just an array of values.
*/
function wc_get_orders( $args ) {
$args = wp_parse_args( $args, array(
'status' => array_keys( wc_get_order_statuses() ),
'type' => wc_get_order_types( 'view-orders' ),
'parent' => null,
'customer' => null,
'email' => '',
'limit' => get_option( 'posts_per_page' ),
'offset' => null,
'page' => 1,
'exclude' => array(),
'orderby' => 'date',
'order' => 'DESC',
'return' => 'objects',
'paginate' => false,
'date_before' => '',
'date_after' => '',
) );
// Handle some BW compatibility arg names where wp_query args differ in naming.
$map_legacy = array(
'numberposts' => 'limit',
'post_type' => 'type',
'post_status' => 'status',
'post_parent' => 'parent',
'author' => 'customer',
'email' => 'billing_email',
'posts_per_page' => 'limit',
'paged' => 'page',
);
@ -82,7 +43,29 @@ function wc_get_orders( $args ) {
}
}
return WC_Data_Store::load( 'order' )->get_orders( $args );
// Map legacy date args to modern date args.
$date_before = false;
$date_after = false;
if ( ! empty( $args['date_before'] ) ) {
$datetime = wc_string_to_datetime( $args['date_before'] );
$date_before = strpos( $args['date_before'], ':' ) ? $datetime->getOffsetTimestamp() : $datetime->date( 'Y-m-d' );
}
if ( ! empty( $args['date_after'] ) ) {
$datetime = wc_string_to_datetime( $args['date_after'] );
$date_after = strpos( $args['date_after'], ':' ) ? $datetime->getOffsetTimestamp() : $datetime->date( 'Y-m-d' );
}
if ( $date_before && $date_after ) {
$args['date_created'] = $date_before . '...' . $date_after;
} elseif ( $date_before ) {
$args['date_created'] = '<' . $date_before;
} elseif ( $date_after ) {
$args['date_created'] = '>' . $date_after;
}
$query = new WC_Order_Query( $args );
return $query->get_orders();
}
/**