From 4fd47e1b02a3878474a782dbbdb7251fdbb4f28d Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 17 Nov 2016 22:11:05 -0200 Subject: [PATCH] Use wc_get_orders() to query customer orders --- .../api/legacy/v1/class-wc-api-customers.php | 21 ++++++++++--------- .../api/legacy/v2/class-wc-api-customers.php | 15 +++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/includes/api/legacy/v1/class-wc-api-customers.php b/includes/api/legacy/v1/class-wc-api-customers.php index 04fe43ee104..3e1e3736244 100644 --- a/includes/api/legacy/v1/class-wc-api-customers.php +++ b/includes/api/legacy/v1/class-wc-api-customers.php @@ -249,20 +249,21 @@ class WC_API_Customers extends WC_API_Resource { $id = $this->validate_request( $id, 'customer', 'read' ); - if ( is_wp_error( $id ) ) + 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 ) ); + $order_ids = wc_get_orders( array( + 'customer' => $id, + 'limit' => -1, + 'orderby' => 'date', + 'order' => 'ASC', + 'return' => 'ids', + ) ); - if ( empty( $order_ids ) ) + if ( empty( $order_ids ) ) { return array( 'orders' => array() ); + } $orders = array(); diff --git a/includes/api/legacy/v2/class-wc-api-customers.php b/includes/api/legacy/v2/class-wc-api-customers.php index b9cdc87b81f..37de6e57a3d 100644 --- a/includes/api/legacy/v2/class-wc-api-customers.php +++ b/includes/api/legacy/v2/class-wc-api-customers.php @@ -461,14 +461,13 @@ class WC_API_Customers extends WC_API_Resource { 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 ) ); + $order_ids = wc_get_orders( array( + 'customer' => $id, + 'limit' => -1, + 'orderby' => 'date', + 'order' => 'ASC', + 'return' => 'ids', + ) ); if ( empty( $order_ids ) ) { return array( 'orders' => array() );