Search first and last names Closes #3536
This commit is contained in:
parent
6dae4bcc0d
commit
aed9a0aceb
|
@ -1596,6 +1596,8 @@ function woocommerce_json_search_customers() {
|
|||
|
||||
$found_customers = array( '' => $default );
|
||||
|
||||
add_action( 'pre_user_query', 'woocommerce_json_search_customer_name' );
|
||||
|
||||
$customers_query = new WP_User_Query( array(
|
||||
'fields' => 'all',
|
||||
'orderby' => 'display_name',
|
||||
|
@ -1603,6 +1605,8 @@ function woocommerce_json_search_customers() {
|
|||
'search_columns' => array( 'ID', 'user_login', 'user_email', 'user_nicename' )
|
||||
) );
|
||||
|
||||
remove_action( 'pre_user_query', 'woocommerce_json_search_customer_name' );
|
||||
|
||||
$customers = $customers_query->get_results();
|
||||
|
||||
if ( $customers ) {
|
||||
|
@ -1617,6 +1621,22 @@ function woocommerce_json_search_customers() {
|
|||
|
||||
add_action('wp_ajax_woocommerce_json_search_customers', 'woocommerce_json_search_customers');
|
||||
|
||||
/**
|
||||
* When searching using the WP_User_Query, search names (user meta) too
|
||||
* @param object $query
|
||||
* @return object
|
||||
*/
|
||||
function woocommerce_json_search_customer_name( $query ) {
|
||||
global $wpdb;
|
||||
|
||||
$term = urldecode( stripslashes( strip_tags( $_GET['term'] ) ) );
|
||||
|
||||
$query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) ";
|
||||
$query->query_from .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id) ";
|
||||
|
||||
$query->query_where .= $wpdb->prepare( " OR ( meta2.meta_value LIKE %s OR meta3.meta_value LIKE %s )", '%' . like_escape( $term ) . '%', '%' . like_escape( $term ) . '%' );
|
||||
$query->query_where .= " AND meta2.meta_key = 'first_name' AND meta3.meta_key = 'last_name' ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax request handling for categories ordering
|
||||
|
|
Loading…
Reference in New Issue