Allow filter orders by customer id, closes #9162
This commit is contained in:
parent
1522a4d87b
commit
f9fb149551
|
@ -461,37 +461,20 @@ class WC_API_Customers extends WC_API_Resource {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
* @param int $id the customer ID
|
* @param int $id the customer ID
|
||||||
* @param string $fields fields to include in response
|
* @param string $fields fields to include in response
|
||||||
|
* @param array $filter filters
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function get_customer_orders( $id, $fields = null ) {
|
public function get_customer_orders( $id, $fields = null, $filter = array() ) {
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
$id = $this->validate_request( $id, 'customer', 'read' );
|
$id = $this->validate_request( $id, 'customer', 'read' );
|
||||||
|
|
||||||
if ( is_wp_error( $id ) ) {
|
if ( is_wp_error( $id ) ) {
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id
|
$filter['customer_id'] = $id;
|
||||||
FROM $wpdb->posts AS posts
|
$orders = WC()->api->WC_API_Orders->get_orders( $fields, $filter, null, -1 );
|
||||||
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
|
|
||||||
WHERE meta.meta_key = '_customer_user'
|
|
||||||
AND meta.meta_value = '%s'
|
|
||||||
AND posts.post_type = 'shop_order'
|
|
||||||
AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' )
|
|
||||||
", $id ) );
|
|
||||||
|
|
||||||
if ( empty( $order_ids ) ) {
|
return $orders;
|
||||||
return array( 'orders' => array() );
|
|
||||||
}
|
|
||||||
|
|
||||||
$orders = array();
|
|
||||||
|
|
||||||
foreach ( $order_ids as $order_id ) {
|
|
||||||
$orders[] = current( WC()->api->WC_API_Orders->get_order( $order_id, $fields ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return array( 'orders' => apply_filters( 'woocommerce_api_customer_orders_response', $orders, $id, $fields, $order_ids, $this->server ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -711,13 +711,21 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
|
|
||||||
// add status argument
|
// add status argument
|
||||||
if ( ! empty( $args['status'] ) ) {
|
if ( ! empty( $args['status'] ) ) {
|
||||||
|
|
||||||
$statuses = 'wc-' . str_replace( ',', ',wc-', $args['status'] );
|
$statuses = 'wc-' . str_replace( ',', ',wc-', $args['status'] );
|
||||||
$statuses = explode( ',', $statuses );
|
$statuses = explode( ',', $statuses );
|
||||||
$query_args['post_status'] = $statuses;
|
$query_args['post_status'] = $statuses;
|
||||||
|
|
||||||
unset( $args['status'] );
|
unset( $args['status'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $args['customer_id'] ) ) {
|
||||||
|
$query_args['meta_query'] = array(
|
||||||
|
array(
|
||||||
|
'key' => '_customer_user',
|
||||||
|
'value' => absint( $args['customer_id'] ),
|
||||||
|
'compare' => '='
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query_args = $this->merge_query_args( $query_args, $args );
|
$query_args = $this->merge_query_args( $query_args, $args );
|
||||||
|
|
Loading…
Reference in New Issue