|
| 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 ); |
| 19 | + |
| 20 | +function wc_search_customers() { |
| 21 | +ob_start(); |
| 22 | + |
| 23 | + check_ajax_referer( 'search-customers', 'security' ); |
| 24 | + |
| 25 | + if ( ! current_user_can( 'edit_shop_orders' ) ) { |
| 26 | + die(-1); |
| 27 | + } |
| 28 | + |
| 29 | + $term = wc_clean( stripslashes( $_GET['term'] ) ); |
| 30 | + $exclude = array(); |
| 31 | + |
| 32 | + if ( empty( $term ) ) { |
| 33 | + die(); |
| 34 | + } |
| 35 | + |
| 36 | + if ( ! empty( $_GET['exclude'] ) ) { |
| 37 | + $exclude = array_map( 'intval', explode( ',', $_GET['exclude'] ) ); |
| 38 | + } |
| 39 | + |
| 40 | + $found_customers = array(); |
| 41 | + |
| 42 | + |
| 43 | + $customers_query = new WP_User_Query( apply_filters( 'woocommerce_json_search_customers_query', array( |
| 44 | + 'fields' => 'all', |
| 45 | + 'orderby' => 'display_name', |
| 46 | + 'search' => '*' . $term . '*', |
| 47 | + 'search_columns' => array( 'ID', 'user_login', 'user_email', 'user_nicename' ) |
| 48 | + ) ) ); |
| 49 | + |
| 50 | +error_log( print_r( $customers_query, true ) ); |
| 51 | + $customers = $customers_query->get_results(); |
| 52 | + |
| 53 | + if ( ! empty( $customers ) ) { |
| 54 | + foreach ( $customers as $customer ) { |
| 55 | + if ( ! in_array( $customer->ID, $exclude ) ) { |
| 56 | + $found_customers[ $customer->ID ] = $customer->display_name . ' (#' . $customer->ID . ' &ndash; ' . sanitize_email( $customer->user_email ) . ')'; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + $found_customers = apply_filters( 'woocommerce_json_search_found_customers', $found_customers ); |
| 62 | + |
| 63 | + wp_send_json( $found_customers ); |
| 64 | +}</code> |
| 65 | + </snippet> |
| 66 | +</snippets> |
0 commit comments