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.
|
||||
*
|
||||
* @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 {
|
||||
|
||||
// @todo Add support for guests here. See https://wp.me/p7bje6-1dM.
|
||||
class WC_Admin_REST_Customers_Controller extends WC_Admin_REST_Reports_Customers_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v4';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
protected $rest_base = 'customers';
|
||||
}
|
||||
|
||||
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['orderby'] = $request['orderby'];
|
||||
$args['match'] = $request['match'];
|
||||
$args['name'] = $request['name'];
|
||||
$args['search'] = $request['search'];
|
||||
$args['username'] = $request['username'];
|
||||
$args['email'] = $request['email'];
|
||||
$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',
|
||||
);
|
||||
$params['name'] = array(
|
||||
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
|
||||
$params['search'] = array(
|
||||
'description' => __( 'Limit response to objects with a customer name containing the search term.', 'wc-admin' ),
|
||||
'type' => 'string',
|
||||
'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_after'] = $request['registered_after'];
|
||||
$args['match'] = $request['match'];
|
||||
$args['name'] = $request['name'];
|
||||
$args['search'] = $request['search'];
|
||||
$args['username'] = $request['username'];
|
||||
$args['email'] = $request['email'];
|
||||
$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',
|
||||
);
|
||||
$params['name'] = array(
|
||||
$params['search'] = array(
|
||||
'description' => __( 'Limit response to objects with a specfic customer name.', 'wc-admin' ),
|
||||
'type' => 'string',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
|
|
|
@ -173,7 +173,6 @@ class WC_Admin_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-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-countries-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-stock-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(
|
||||
'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'] ) ) {
|
||||
$where_clauses[] = $wpdb->prepare( "CONCAT_WS( ' ', first_name, last_name ) = %s", $query_args['name'] );
|
||||
if ( ! empty( $query_args['search'] ) ) {
|
||||
$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(
|
||||
|
|
|
@ -16,8 +16,6 @@ import { stringifyQuery } from '@woocommerce/navigation';
|
|||
*/
|
||||
import { computeSuggestionMatch } from './utils';
|
||||
|
||||
const getName = customer => [ customer.first_name, customer.last_name ].filter( Boolean ).join( ' ' );
|
||||
|
||||
/**
|
||||
* A customer completer.
|
||||
* See https://github.com/WordPress/gutenberg/tree/master/packages/components/src/autocomplete#the-completer-interface
|
||||
|
@ -40,7 +38,7 @@ export default {
|
|||
},
|
||||
isDebounced: true,
|
||||
getOptionKeywords( customer ) {
|
||||
return [ getName( customer ) ];
|
||||
return [ customer.name ];
|
||||
},
|
||||
getFreeTextOptions( query ) {
|
||||
const label = (
|
||||
|
@ -56,15 +54,15 @@ export default {
|
|||
const nameOption = {
|
||||
key: 'name',
|
||||
label: label,
|
||||
value: { id: query, first_name: query },
|
||||
value: { id: query, name: query },
|
||||
};
|
||||
|
||||
return [ nameOption ];
|
||||
},
|
||||
getOptionLabel( customer, query ) {
|
||||
const match = computeSuggestionMatch( getName( customer ), query ) || {};
|
||||
const match = computeSuggestionMatch( customer.name, query ) || {};
|
||||
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 }
|
||||
<strong className="components-form-token-field__suggestion-match">
|
||||
{ match.suggestionMatch }
|
||||
|
@ -78,7 +76,7 @@ export default {
|
|||
getOptionCompletion( customer ) {
|
||||
return {
|
||||
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).
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'name' => 'Nota Customername',
|
||||
'search' => 'Nota Customername',
|
||||
)
|
||||
);
|
||||
$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.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'name' => 'Jeff',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
'search' => 'Jeff',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
)
|
||||
);
|
||||
$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).
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'name' => 'Nota Customername',
|
||||
'search' => 'Nota Customername',
|
||||
)
|
||||
);
|
||||
$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.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'name' => 'Justin',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
'search' => 'Justin',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
|
Loading…
Reference in New Issue