Skip to content

Commit

Permalink
Merge branch 'wc-get-orders-10500'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Mar 14, 2016
2 parents df75080 + a588757 commit ee07980
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 160 deletions.
44 changes: 11 additions & 33 deletions includes/admin/reports/class-wc-report-customer-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,14 @@ public function column_default( $user, $column_name ) {

case 'last_order' :

$order_ids = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'shop_order',
'orderby' => 'date',
'order' => 'desc',
'post_status' => array( 'wc-completed', 'wc-processing' ),
'meta_query' => array(
array(
'key' => '_customer_user',
'value' => $user->ID
)
),
'fields' => 'ids'
$orders = wc_get_orders( array(
'limit' => 1,
'status' => array( 'wc-completed', 'wc-processing' ),
'customer' => $user->ID
) );

if ( $order_ids ) {
$order = wc_get_order( $order_ids[0] );

if ( ! empty( $orders ) ) {
$order = $orders[0];
return '<a href="' . admin_url( 'post.php?post=' . $order->id . '&action=edit' ) . '">' . _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() . '</a> &ndash; ' . date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) );
} else {
return '-';
Expand Down Expand Up @@ -176,25 +166,13 @@ public function column_default( $user, $column_name ) {
'action' => "view"
);

$order_ids = get_posts( array(
'posts_per_page' => 1,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
'meta_query' => array(
array(
'key' => '_customer_user',
'value' => array( 0, '' ),
'compare' => 'IN'
),
array(
'key' => '_billing_email',
'value' => $user->user_email
)
),
'fields' => 'ids'
$orders = wc_get_orders( array(
'limit' => 1,
'status' => array( 'wc-completed', 'wc-processing' ),
'customer' => array( array( 0, $user->user_email ) ),
) );

if ( $order_ids ) {
if ( $orders ) {
$actions['link'] = array(
'url' => wp_nonce_url( add_query_arg( 'link_orders', $user->ID ), 'link_orders' ),
'name' => __( 'Link previous orders', 'woocommerce' ),
Expand Down
17 changes: 7 additions & 10 deletions includes/api/class-wc-api-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -1510,15 +1510,12 @@ public function get_order_refunds( $order_id, $fields = null ) {
return $order_id;
}

$refund_items = get_posts(
array(
'post_type' => 'shop_order_refund',
'post_parent' => $order_id,
'posts_per_page' => -1,
'post_status' => 'any',
'fields' => 'ids'
)
);
$refund_items = wc_get_orders( array(
'type' => 'shop_order_refund',
'parent' => $order_id,
'limit' => -1,
'return' => 'ids',
) );
$order_refunds = array();

foreach ( $refund_items as $refund_id ) {
Expand Down Expand Up @@ -1551,7 +1548,7 @@ public function get_order_refund( $order_id, $id, $fields = null ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), 400 );
}

$order = wc_get_order( $id );
$order = wc_get_order( $order_id );
$refund = wc_get_order( $id );

if ( ! $refund ) {
Expand Down
17 changes: 7 additions & 10 deletions includes/api/v2/class-wc-api-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -1466,15 +1466,12 @@ public function get_order_refunds( $order_id, $fields = null ) {
return $order_id;
}

$refund_items = get_posts(
array(
'post_type' => 'shop_order_refund',
'post_parent' => $order_id,
'posts_per_page' => -1,
'post_status' => 'any',
'fields' => 'ids'
)
);
$refund_items = wc_get_orders( array(
'type' => 'shop_order_refund',
'parent' => $order_id,
'limit' => -1,
'return' => 'ids',
) );
$order_refunds = array();

foreach ( $refund_items as $refund_id ) {
Expand Down Expand Up @@ -1507,7 +1504,7 @@ public function get_order_refund( $order_id, $id, $fields = null ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), 400 );
}

$order = wc_get_order( $id );
$order = wc_get_order( $order_id );
$refund = wc_get_order( $id );

if ( ! $refund ) {
Expand Down
21 changes: 5 additions & 16 deletions includes/class-wc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ public function get_formatted_order_total( $tax_display = '', $display_refunded
*/
public function get_refunds() {
if ( empty( $this->refunds ) && ! is_array( $this->refunds ) ) {
$refunds = array();
$refund_items = get_posts(
array(
'post_type' => 'shop_order_refund',
'post_parent' => $this->id,
'posts_per_page' => -1,
'post_status' => 'any',
'fields' => 'ids'
)
);

foreach ( $refund_items as $refund_id ) {
$refunds[] = new WC_Order_Refund( $refund_id );
}

$this->refunds = $refunds;
$this->refunds = wc_get_orders( array(
'type' => 'shop_order_refund',
'parent' => $this->id,
'limit' => -1,
) );
}
return $this->refunds;
}
Expand Down
30 changes: 0 additions & 30 deletions includes/wc-account-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,6 @@ function wc_get_account_orders_columns() {
return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns );
}

/**
* Get My Account > Orders query args.
*
* @since 2.6.0
* @param int $current_page
* @return array
*/
function wc_get_account_orders_query_args( $current_page = 1 ) {
$args = array(
'numberposts' => 15,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
);

// @deprecated 2.6.0.
$args = apply_filters( 'woocommerce_my_account_my_orders_query', $args );

// Remove deprecated option.
$args['posts_per_page'] = $args['numberposts'];
unset( $args['numberposts'] );

if ( 1 < $current_page ) {
$args['paged'] = absint( $current_page );
}

return apply_filters( 'woocommerce_account_orders_query', $args );
}

/**
* Get My Account > Downloads columns.
*
Expand Down
167 changes: 167 additions & 0 deletions includes/wc-order-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,173 @@
exit; // Exit if accessed directly
}

/**
* Wrapper for get_posts specific to orders.
*
* 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. [email protected], 1, array( 1, 2, 3 ), array( array( 1, '[email protected]' ), 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.
* 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
*
* @since 2.6.0
* @param array $args Array of args (above)
* @return array|stdClass Number of pages and an array of order objects if
* 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,
) );

// 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',
'posts_per_page' => 'limit',
'paged' => 'page',
);

foreach ( $map_legacy as $from => $to ) {
if ( isset( $args[ $from ] ) ) {
$args[ $to ] = $args[ $from ];
}
}

/**
* 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 ( ! empty( $args['customer'] ) ) {
$values = is_array( $args['customer'] ) ? $args['customer'] : array( $args['customer'] );
$wp_query_args['meta_query'][] = _wc_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;
}

// Get results.
$orders = new WP_Query( $wp_query_args );

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

/**
* Generate meta query for wc_get_orders. Used internally only.
* @since 2.6.0
* @param array $values
* @param string $relation
* @return array
*/
function _wc_get_orders_generate_customer_meta_query( $values, $relation = 'or' ) {
$meta_query = array(
'relation' => strtoupper( $relation ),
'customer_emails' => array(
'key' => '_billing_email',
'value' => array(),
'compare' => 'IN',
),
'customer_ids' => array(
'key' => '_customer_user',
'value' => array(),
'compare' => 'IN',
)
);
foreach ( $values as $value ) {
if ( is_array( $value ) ) {
$meta_query[] = _wc_get_orders_generate_customer_meta_query( $value, 'and' );
} elseif ( is_email( $value ) ) {
$meta_query['customer_emails']['value'][] = sanitize_email( $value );
} else {
$meta_query['customer_ids']['value'][] = strval( absint( $value ) );
}
}

if ( empty( $meta_query['customer_emails']['value'] ) ) {
unset( $meta_query['customer_emails'] );
unset( $meta_query['relation'] );
}

if ( empty( $meta_query['customer_ids']['value'] ) ) {
unset( $meta_query['customer_ids'] );
unset( $meta_query['relation'] );
}

return $meta_query;
}

/**
* Get all order statuses.
*
Expand Down
5 changes: 3 additions & 2 deletions includes/wc-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,10 @@ function wc_dropdown_variation_attribute_options( $args = array() ) {
* @param int $current_page Current page number.
*/
function woocommerce_account_orders( $current_page ) {
$current_page = empty( $current_page ) ? 1 : $current_page;
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array( 'customer' => get_current_user_id(), 'page' => $current_page, 'paginate' => true ) ) );

wc_get_template( 'myaccount/orders.php', array( 'current_page' => absint( $current_page ) ) );
wc_get_template( 'myaccount/orders.php', array( 'current_page' => absint( $current_page ), 'customer_orders' => $customer_orders ) );
}
}

Expand Down
Loading

0 comments on commit ee07980

Please sign in to comment.