WC_Customer_Data_Store::search_customers()publicWC 3.0.7

Search customers and return customer IDs.

Method of the class: WC_Customer_Data_Store{}

Return

Array.

Usage

$WC_Customer_Data_Store = new WC_Customer_Data_Store();
$WC_Customer_Data_Store->search_customers( $term, $limit );
$term(string) (required)
Search term.
$limit(int|string)
Limit search results.
Default: ''

Changelog

Since 3.0.7 Introduced.

WC_Customer_Data_Store::search_customers() code WC 9.4.2

public function search_customers( $term, $limit = '' ) {
	$results = apply_filters( 'woocommerce_customer_pre_search_customers', false, $term, $limit );
	if ( is_array( $results ) ) {
		return $results;
	}

	$query = new WP_User_Query(
		apply_filters(
			'woocommerce_customer_search_customers',
			array(
				'search'         => '*' . esc_attr( $term ) . '*',
				'search_columns' => array( 'user_login', 'user_url', 'user_email', 'user_nicename', 'display_name' ),
				'fields'         => 'ID',
				'number'         => $limit,
			),
			$term,
			$limit,
			'main_query'
		)
	);

	$query2 = new WP_User_Query(
		apply_filters(
			'woocommerce_customer_search_customers',
			array(
				'fields'     => 'ID',
				'number'     => $limit,
				'meta_query' => array(
					'relation' => 'OR',
					array(
						'key'     => 'first_name',
						'value'   => $term,
						'compare' => 'LIKE',
					),
					array(
						'key'     => 'last_name',
						'value'   => $term,
						'compare' => 'LIKE',
					),
				),
			),
			$term,
			$limit,
			'meta_query'
		)
	);

	$results = wp_parse_id_list( array_merge( (array) $query->get_results(), (array) $query2->get_results() ) );

	if ( $limit && count( $results ) > $limit ) {
		$results = array_slice( $results, 0, $limit );
	}

	return $results;
}