Hook `v4/customers` endpoint up to customers report/lookup table and change name search to be fuzzy.
This commit is contained in:
parent
1c990aec04
commit
926fdcfb97
|
@ -13,46 +13,14 @@ defined( 'ABSPATH' ) || exit;
|
||||||
* Customers controller.
|
* Customers controller.
|
||||||
*
|
*
|
||||||
* @package WooCommerce Admin/API
|
* @package WooCommerce Admin/API
|
||||||
* @extends WC_REST_Customers_Controller
|
* @extends WC_Admin_REST_Reports_Customers_Controller
|
||||||
*/
|
*/
|
||||||
class WC_Admin_REST_Customers_Controller extends WC_REST_Customers_Controller {
|
class WC_Admin_REST_Customers_Controller extends WC_Admin_REST_Reports_Customers_Controller {
|
||||||
|
|
||||||
// @todo Add support for guests here. See https://wp.me/p7bje6-1dM.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Endpoint namespace.
|
* Route base.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $namespace = 'wc/v4';
|
protected $rest_base = 'customers';
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches emails by partial search instead of a strict match.
|
|
||||||
* See "search parameters" under https://codex.wordpress.org/Class_Reference/WP_User_Query.
|
|
||||||
*
|
|
||||||
* @param array $prepared_args Prepared search filter args from the customer endpoint.
|
|
||||||
* @param array $request Request/query arguments.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public static function update_search_filters( $prepared_args, $request ) {
|
|
||||||
if ( ! empty( $request['email'] ) ) {
|
|
||||||
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
|
|
||||||
}
|
|
||||||
return $prepared_args;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the query params for collections.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function get_collection_params() {
|
|
||||||
$params = parent::get_collection_params();
|
|
||||||
// Allow partial email matches. Previously, this was of format 'email' which required a strict "test@example.com" format.
|
|
||||||
// This, in combination with `update_search_filters` allows us to do partial searches.
|
|
||||||
$params['email']['format'] = '';
|
|
||||||
return $params;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_filter( 'woocommerce_rest_customer_query', array( 'WC_Admin_REST_Customers_Controller', 'update_search_filters' ), 10, 2 );
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
|
||||||
$args['order'] = $request['order'];
|
$args['order'] = $request['order'];
|
||||||
$args['orderby'] = $request['orderby'];
|
$args['orderby'] = $request['orderby'];
|
||||||
$args['match'] = $request['match'];
|
$args['match'] = $request['match'];
|
||||||
$args['name'] = $request['name'];
|
$args['search'] = $request['search'];
|
||||||
$args['username'] = $request['username'];
|
$args['username'] = $request['username'];
|
||||||
$args['email'] = $request['email'];
|
$args['email'] = $request['email'];
|
||||||
$args['country'] = $request['country'];
|
$args['country'] = $request['country'];
|
||||||
|
@ -333,8 +333,8 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
|
||||||
),
|
),
|
||||||
'validate_callback' => 'rest_validate_request_arg',
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
);
|
);
|
||||||
$params['name'] = array(
|
$params['search'] = array(
|
||||||
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
|
'description' => __( 'Limit response to objects with a customer name containing the search term.', 'wc-admin' ),
|
||||||
'type' => 'string',
|
'type' => 'string',
|
||||||
'validate_callback' => 'rest_validate_request_arg',
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
);
|
);
|
||||||
|
|
|
@ -41,7 +41,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
|
||||||
$args['registered_before'] = $request['registered_before'];
|
$args['registered_before'] = $request['registered_before'];
|
||||||
$args['registered_after'] = $request['registered_after'];
|
$args['registered_after'] = $request['registered_after'];
|
||||||
$args['match'] = $request['match'];
|
$args['match'] = $request['match'];
|
||||||
$args['name'] = $request['name'];
|
$args['search'] = $request['search'];
|
||||||
$args['username'] = $request['username'];
|
$args['username'] = $request['username'];
|
||||||
$args['email'] = $request['email'];
|
$args['email'] = $request['email'];
|
||||||
$args['country'] = $request['country'];
|
$args['country'] = $request['country'];
|
||||||
|
@ -246,7 +246,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
|
||||||
),
|
),
|
||||||
'validate_callback' => 'rest_validate_request_arg',
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
);
|
);
|
||||||
$params['name'] = array(
|
$params['search'] = array(
|
||||||
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
|
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
|
||||||
'type' => 'string',
|
'type' => 'string',
|
||||||
'validate_callback' => 'rest_validate_request_arg',
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
|
|
|
@ -173,7 +173,6 @@ class WC_Admin_Api_Init {
|
||||||
public function rest_api_init() {
|
public function rest_api_init() {
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-admin-notes-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-admin-notes-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-coupons-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-coupons-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-customers-controller.php';
|
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-countries-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-countries-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-download-ips-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-data-download-ips-controller.php';
|
||||||
|
@ -205,6 +204,7 @@ class WC_Admin_Api_Init {
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-taxes-stats-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-taxes-stats-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-stock-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-reports-stock-controller.php';
|
||||||
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-taxes-controller.php';
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-taxes-controller.php';
|
||||||
|
require_once dirname( __FILE__ ) . '/api/class-wc-admin-rest-customers-controller.php';
|
||||||
|
|
||||||
$controllers = apply_filters(
|
$controllers = apply_filters(
|
||||||
'woocommerce_admin_rest_controllers',
|
'woocommerce_admin_rest_controllers',
|
||||||
|
|
|
@ -230,8 +230,10 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! empty( $query_args['name'] ) ) {
|
if ( ! empty( $query_args['search'] ) ) {
|
||||||
$where_clauses[] = $wpdb->prepare( "CONCAT_WS( ' ', first_name, last_name ) = %s", $query_args['name'] );
|
$name_like = '%' . $wpdb->esc_like( $query_args['search'] ) . '%';
|
||||||
|
$where_clauses[] = $wpdb->prepare( "CONCAT_WS( ' ', first_name, last_name ) LIKE %s", $name_like );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$numeric_params = array(
|
$numeric_params = array(
|
||||||
|
|
|
@ -16,8 +16,6 @@ import { stringifyQuery } from '@woocommerce/navigation';
|
||||||
*/
|
*/
|
||||||
import { computeSuggestionMatch } from './utils';
|
import { computeSuggestionMatch } from './utils';
|
||||||
|
|
||||||
const getName = customer => [ customer.first_name, customer.last_name ].filter( Boolean ).join( ' ' );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A customer completer.
|
* A customer completer.
|
||||||
* See https://github.com/WordPress/gutenberg/tree/master/packages/components/src/autocomplete#the-completer-interface
|
* See https://github.com/WordPress/gutenberg/tree/master/packages/components/src/autocomplete#the-completer-interface
|
||||||
|
@ -40,7 +38,7 @@ export default {
|
||||||
},
|
},
|
||||||
isDebounced: true,
|
isDebounced: true,
|
||||||
getOptionKeywords( customer ) {
|
getOptionKeywords( customer ) {
|
||||||
return [ getName( customer ) ];
|
return [ customer.name ];
|
||||||
},
|
},
|
||||||
getFreeTextOptions( query ) {
|
getFreeTextOptions( query ) {
|
||||||
const label = (
|
const label = (
|
||||||
|
@ -56,15 +54,15 @@ export default {
|
||||||
const nameOption = {
|
const nameOption = {
|
||||||
key: 'name',
|
key: 'name',
|
||||||
label: label,
|
label: label,
|
||||||
value: { id: query, first_name: query },
|
value: { id: query, name: query },
|
||||||
};
|
};
|
||||||
|
|
||||||
return [ nameOption ];
|
return [ nameOption ];
|
||||||
},
|
},
|
||||||
getOptionLabel( customer, query ) {
|
getOptionLabel( customer, query ) {
|
||||||
const match = computeSuggestionMatch( getName( customer ), query ) || {};
|
const match = computeSuggestionMatch( customer.name, query ) || {};
|
||||||
return [
|
return [
|
||||||
<span key="name" className="woocommerce-search__result-name" aria-label={ getName( customer ) }>
|
<span key="name" className="woocommerce-search__result-name" aria-label={ customer.name }>
|
||||||
{ match.suggestionBeforeMatch }
|
{ match.suggestionBeforeMatch }
|
||||||
<strong className="components-form-token-field__suggestion-match">
|
<strong className="components-form-token-field__suggestion-match">
|
||||||
{ match.suggestionMatch }
|
{ match.suggestionMatch }
|
||||||
|
@ -78,7 +76,7 @@ export default {
|
||||||
getOptionCompletion( customer ) {
|
getOptionCompletion( customer ) {
|
||||||
return {
|
return {
|
||||||
id: customer.id,
|
id: customer.id,
|
||||||
label: getName( customer ),
|
label: customer.name,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -142,7 +142,7 @@ class WC_Tests_API_Reports_Customers_Stats extends WC_REST_Unit_Test_Case {
|
||||||
// Test name parameter (case with no matches).
|
// Test name parameter (case with no matches).
|
||||||
$request->set_query_params(
|
$request->set_query_params(
|
||||||
array(
|
array(
|
||||||
'name' => 'Nota Customername',
|
'search' => 'Nota Customername',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$response = $this->server->dispatch( $request );
|
$response = $this->server->dispatch( $request );
|
||||||
|
@ -157,8 +157,8 @@ class WC_Tests_API_Reports_Customers_Stats extends WC_REST_Unit_Test_Case {
|
||||||
// Test name and last_order parameters.
|
// Test name and last_order parameters.
|
||||||
$request->set_query_params(
|
$request->set_query_params(
|
||||||
array(
|
array(
|
||||||
'name' => 'Jeff',
|
'search' => 'Jeff',
|
||||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$response = $this->server->dispatch( $request );
|
$response = $this->server->dispatch( $request );
|
||||||
|
|
|
@ -163,7 +163,7 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
|
||||||
// Test name parameter (case with no matches).
|
// Test name parameter (case with no matches).
|
||||||
$request->set_query_params(
|
$request->set_query_params(
|
||||||
array(
|
array(
|
||||||
'name' => 'Nota Customername',
|
'search' => 'Nota Customername',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$response = $this->server->dispatch( $request );
|
$response = $this->server->dispatch( $request );
|
||||||
|
@ -175,8 +175,8 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
|
||||||
// Test name and last_order parameters.
|
// Test name and last_order parameters.
|
||||||
$request->set_query_params(
|
$request->set_query_params(
|
||||||
array(
|
array(
|
||||||
'name' => 'Justin',
|
'search' => 'Justin',
|
||||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$response = $this->server->dispatch( $request );
|
$response = $this->server->dispatch( $request );
|
||||||
|
|
Loading…
Reference in New Issue