diff --git a/includes/api/class-wc-api-customers.php b/includes/api/class-wc-api-customers.php index 79f15279f75..fba881cd883 100644 --- a/includes/api/class-wc-api-customers.php +++ b/includes/api/class-wc-api-customers.php @@ -70,6 +70,11 @@ class WC_API_Customers extends WC_API_Resource { array( array( $this, 'get_customer' ), WC_API_SERVER::READABLE ), ); + # GET /customers/ + $routes[ $this->base . '/email/(?P.+)' ] = array( + array( array( $this, 'get_customer_by_email' ), WC_API_SERVER::READABLE ), + ); + # GET /customers//orders $routes[ $this->base . '/(?P\d+)/orders' ] = array( array( array( $this, 'get_customer_orders' ), WC_API_SERVER::READABLE ), @@ -177,6 +182,28 @@ class WC_API_Customers extends WC_API_Resource { return array( 'customer' => apply_filters( 'woocommerce_api_customer_response', $customer_data, $customer, $fields, $this->server ) ); } + /** + * Get the customer for the given email + * + * @since 2.2 + * @param string $email the customer email + * @param string $fields + * @return array + */ + function get_customer_by_email( $email, $fields = null ) { + + if ( is_email( $email ) ) { + $customer = get_user_by( 'email', $email ); + if ( ! is_object( $customer ) ) { + return new WP_Error( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), array( 'status' => 404 ) ); + } + } else { + return new WP_Error( 'woocommerce_api_invalid_customer_email', __( 'Invalid customer Email', 'woocommerce' ), array( 'status' => 404 ) ); + } + + return $this->get_customer( $customer->ID, $fields ); + } + /** * Get the total number of customers *