Skip to content

Commit f56e872

Browse files
authored
Search customers with numeric usernames in WooCommerce Add
1 parent f51fee7 commit f56e872

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- This is a code snippets export file generated by the Code Snippets WordPress plugin. -->
3+
<!-- https://wordpress.org/plugins/code-snippets -->
4+
<!-- To import these snippets a WordPress site follow these steps: -->
5+
<!-- 1. Log in to that site as an administrator. -->
6+
<!-- 2. Install the Code Snippets plugin using the directions provided at the above link. -->
7+
<!-- 3. Go to 'Tools: Import' in the WordPress admin panel. -->
8+
<!-- 4. Click on the "Code Snippets" importer in the list -->
9+
<!-- 5. Upload this file using the form provided on that page. -->
10+
<!-- 6. Code Snippets will then import all of the snippets and associated information contained in this file into your site. -->
11+
<!-- 7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets. -->
12+
<!-- generator="Code Snippets/2.9.4" created="2017-10-06 20:05" -->
13+
<snippets>
14+
<snippet scope="1">
15+
<name>Search customers with numeric usernames in WooCommerce</name>
16+
<desc></desc>
17+
<tags>search, customers, numeric, woocommerce</tags>
18+
<code>add_action( 'wp_ajax_woocommerce_json_search_customers', 'wc_search_customers', 5 );&#13;
19+
&#13;
20+
function wc_search_customers() {&#13;
21+
ob_start();&#13;
22+
&#13;
23+
check_ajax_referer( 'search-customers', 'security' );&#13;
24+
&#13;
25+
if ( ! current_user_can( 'edit_shop_orders' ) ) {&#13;
26+
die(-1);&#13;
27+
}&#13;
28+
&#13;
29+
$term = wc_clean( stripslashes( $_GET['term'] ) );&#13;
30+
$exclude = array();&#13;
31+
&#13;
32+
if ( empty( $term ) ) {&#13;
33+
die();&#13;
34+
}&#13;
35+
&#13;
36+
if ( ! empty( $_GET['exclude'] ) ) {&#13;
37+
$exclude = array_map( 'intval', explode( ',', $_GET['exclude'] ) );&#13;
38+
}&#13;
39+
&#13;
40+
$found_customers = array();&#13;
41+
&#13;
42+
&#13;
43+
$customers_query = new WP_User_Query( apply_filters( 'woocommerce_json_search_customers_query', array(&#13;
44+
'fields' =&gt; 'all',&#13;
45+
'orderby' =&gt; 'display_name',&#13;
46+
'search' =&gt; '*' . $term . '*',&#13;
47+
'search_columns' =&gt; array( 'ID', 'user_login', 'user_email', 'user_nicename' )&#13;
48+
) ) );&#13;
49+
&#13;
50+
error_log( print_r( $customers_query, true ) );&#13;
51+
$customers = $customers_query-&gt;get_results();&#13;
52+
&#13;
53+
if ( ! empty( $customers ) ) {&#13;
54+
foreach ( $customers as $customer ) {&#13;
55+
if ( ! in_array( $customer-&gt;ID, $exclude ) ) {&#13;
56+
$found_customers[ $customer-&gt;ID ] = $customer-&gt;display_name . ' (#' . $customer-&gt;ID . ' &amp;ndash; ' . sanitize_email( $customer-&gt;user_email ) . ')';&#13;
57+
}&#13;
58+
}&#13;
59+
}&#13;
60+
&#13;
61+
$found_customers = apply_filters( 'woocommerce_json_search_found_customers', $found_customers );&#13;
62+
&#13;
63+
wp_send_json( $found_customers );&#13;
64+
}</code>
65+
</snippet>
66+
</snippets>

0 commit comments

Comments
 (0)