Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1534 from woocommerce/fix/1455-customers-report-n…
Browse files Browse the repository at this point in the history
…ame-search

Hook up customers report table search box
  • Loading branch information
jeffstieler authored Feb 13, 2019
2 parents ca6db3b + 0c99837 commit 835bb8b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 63 deletions.
2 changes: 1 addition & 1 deletion client/analytics/report/customers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const advancedFilters = {
type: 'customers',
getLabels: getRequestByIdString( NAMESPACE + '/customers', customer => ( {
id: customer.id,
label: [ customer.first_name, customer.last_name ].filter( Boolean ).join( ' ' ),
label: customer.name,
} ) ),
},
},
Expand Down
40 changes: 4 additions & 36 deletions includes/api/class-wc-admin-rest-customers-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,14 @@
* 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 );
20 changes: 16 additions & 4 deletions includes/api/class-wc-admin-rest-reports-customers-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function prepare_reports_query( $request ) {
$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'];
Expand All @@ -60,6 +60,7 @@ protected function prepare_reports_query( $request ) {
$args['avg_order_value_max'] = $request['avg_order_value_max'];
$args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after'];
$args['customers'] = $request['customers'];

$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false );
Expand Down Expand Up @@ -172,7 +173,7 @@ public function get_item_schema() {
'title' => 'report_customers',
'type' => 'object',
'properties' => array(
'customer_id' => array(
'id' => array(
'description' => __( 'Customer ID.', 'wc-admin' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
Expand Down Expand Up @@ -333,8 +334,8 @@ public function get_collection_params() {
),
'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',
);
Expand Down Expand Up @@ -446,6 +447,17 @@ public function get_collection_params() {
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['customers'] = array(
'description' => __( 'Limit result to items with specified customer ids.', 'wc-admin' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),

);

return $params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function prepare_reports_query( $request ) {
$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'];
Expand All @@ -55,6 +55,7 @@ protected function prepare_reports_query( $request ) {
$args['avg_order_value_max'] = $request['avg_order_value_max'];
$args['last_order_before'] = $request['last_order_before'];
$args['last_order_after'] = $request['last_order_after'];
$args['customers'] = $request['customers'];

$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized_params_numeric = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params_numeric, false );
Expand Down Expand Up @@ -246,7 +247,7 @@ public function get_collection_params() {
),
'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',
Expand Down Expand Up @@ -359,6 +360,16 @@ public function get_collection_params() {
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['customers'] = array(
'description' => __( 'Limit result to items with specified customer ids.', 'wc-admin' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_id_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'integer',
),

);

return $params;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-admin-api-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public function init_classes() {
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';
Expand Down Expand Up @@ -132,6 +131,7 @@ public function rest_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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* @var array
*/
protected $column_types = array(
'customer_id' => 'intval',
'id' => 'intval',
'user_id' => 'intval',
'orders_count' => 'intval',
'total_spend' => 'floatval',
Expand All @@ -38,7 +38,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
* @var array
*/
protected $report_columns = array(
'customer_id' => 'customer_id',
'id' => 'customer_id as id',
'user_id' => 'user_id',
'username' => 'username',
'name' => "CONCAT_WS( ' ', first_name, last_name ) as name", // @todo What does this mean for RTL?
Expand All @@ -60,7 +60,7 @@ public function __construct() {
global $wpdb;

// Initialize some report columns that need disambiguation.
$this->report_columns['customer_id'] = $wpdb->prefix . self::TABLE_NAME . '.customer_id';
$this->report_columns['id'] = $wpdb->prefix . self::TABLE_NAME . '.customer_id as id';
$this->report_columns['date_last_order'] = "MAX( {$wpdb->prefix}wc_order_stats.date_created ) as date_last_order";
}

Expand Down Expand Up @@ -230,8 +230,15 @@ protected function get_sql_query_params( $query_args ) {
}
}

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 );
}

// Allow a list of customer IDs to be specified.
if ( ! empty( $query_args['customers'] ) ) {
$included_customers = implode( ',', $query_args['customers'] );
$where_clauses[] = "{$customer_lookup_table}.customer_id IN ({$included_customers})";
}

$numeric_params = array(
Expand Down
12 changes: 5 additions & 7 deletions packages/components/src/search/autocompleters/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +38,7 @@ export default {
},
isDebounced: true,
getOptionKeywords( customer ) {
return [ getName( customer ) ];
return [ customer.name ];
},
getFreeTextOptions( query ) {
const label = (
Expand All @@ -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 }
Expand All @@ -78,7 +76,7 @@ export default {
getOptionCompletion( customer ) {
return {
id: customer.id,
label: getName( customer ),
label: customer.name,
};
},
};
6 changes: 3 additions & 3 deletions tests/api/reports-customers-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function test_get_reports() {
// Test name parameter (case with no matches).
$request->set_query_params(
array(
'name' => 'Nota Customername',
'search' => 'Nota Customername',
)
);
$response = $this->server->dispatch( $request );
Expand All @@ -157,8 +157,8 @@ public function test_get_reports() {
// 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 );
Expand Down
8 changes: 4 additions & 4 deletions tests/api/reports-customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_register_routes() {
* @param array $schema Item to check schema.
*/
public function assert_report_item_schema( $schema ) {
$this->assertArrayHasKey( 'customer_id', $schema );
$this->assertArrayHasKey( 'id', $schema );
$this->assertArrayHasKey( 'user_id', $schema );
$this->assertArrayHasKey( 'name', $schema );
$this->assertArrayHasKey( 'username', $schema );
Expand Down Expand Up @@ -163,7 +163,7 @@ public function test_get_reports() {
// Test name parameter (case with no matches).
$request->set_query_params(
array(
'name' => 'Nota Customername',
'search' => 'Nota Customername',
)
);
$response = $this->server->dispatch( $request );
Expand All @@ -175,8 +175,8 @@ public function test_get_reports() {
// 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 );
Expand Down

0 comments on commit 835bb8b

Please sign in to comment.