diff --git a/includes/api/class-wc-api-customers.php b/includes/api/class-wc-api-customers.php index ec633492711..2093282d7b8 100644 --- a/includes/api/class-wc-api-customers.php +++ b/includes/api/class-wc-api-customers.php @@ -461,37 +461,20 @@ class WC_API_Customers extends WC_API_Resource { * @since 2.1 * @param int $id the customer ID * @param string $fields fields to include in response + * @param array $filter filters * @return array */ - public function get_customer_orders( $id, $fields = null ) { - global $wpdb; - + public function get_customer_orders( $id, $fields = null, $filter = array() ) { $id = $this->validate_request( $id, 'customer', 'read' ); if ( is_wp_error( $id ) ) { return $id; } - $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id - FROM $wpdb->posts AS posts - 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 ) ); + $filter['customer_id'] = $id; + $orders = WC()->api->WC_API_Orders->get_orders( $fields, $filter, null, -1 ); - if ( empty( $order_ids ) ) { - 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 ) ); + return $orders; } /** diff --git a/includes/api/class-wc-api-orders.php b/includes/api/class-wc-api-orders.php index 1bca40bb787..716a94388fc 100644 --- a/includes/api/class-wc-api-orders.php +++ b/includes/api/class-wc-api-orders.php @@ -711,13 +711,21 @@ class WC_API_Orders extends WC_API_Resource { // add status argument if ( ! empty( $args['status'] ) ) { - $statuses = 'wc-' . str_replace( ',', ',wc-', $args['status'] ); $statuses = explode( ',', $statuses ); $query_args['post_status'] = $statuses; 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 );