Customize autocomplete results
The Phone Orders for WooCommerce plugin includes an autocomplete feature that displays customers and past orders when an agent starts typing in the Find or create a customer search bar. By default, the plugin shows standard information, such as the customer’s name and email. However, you may need to display additional data, such as a loyalty ID, a custom savings identifier, or the billing address, directly inside the dropdown.
The code snippet below shows how to customise the autocomplete results by modifying the displayed title for both customers and existing orders. You can extend it to include any custom meta field or to change the formatting of the text.
1. The Complete Code Snippet
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
if( isset( $_GET['wpo_find_customer'] ) ) { add_filter( "woocommerce_json_search_found_customers", function($customers){ foreach($customers as $key=>$data) { if($data['type'] == 'order' ) { $order = new WC_Order($data['id']); $title = sprintf( esc_html__( '%1$s (#%2$s – %3$s)', 'phone-orders-for-woocommerce' ), implode( ' ', array( current( array_filter( array( $order->get_billing_first_name(), $order->get_shipping_first_name(), ) ) ), current( array_filter( array( $order->get_billing_last_name(), $order->get_shipping_last_name(), ) ) ), ) ), get_user_meta($order->get_customer_id(), "SavingsID", true), $order->get_billing_address_1() ); } elseif($data['type'] == 'customer' ) { $customer = new WC_Customer($data['id']); $title = sprintf( esc_html__( '%1$s (#%2$s – %3$s)', 'phone-orders-for-woocommerce' ), implode( ' ', array( current( array_filter( array( $customer->get_billing_first_name(), $customer->get_shipping_first_name(), ) ) ), current( array_filter( array( $customer->get_billing_last_name(), $customer->get_shipping_last_name(), ) ) ), ) ), get_user_meta($data['id'], "SavingsID", true), $customer->get_billing_address_1() ); } $customers[$key]['title'] = $title; } return $customers; },100); } |
Note: The code runs only when the
wpo_find_customerparameter is present in the URL, which is automatically added during customer searches. This ensures the filter does not affect other WooCommerce JSON search operations on your site.
2. How the Code Works
The snippet attaches a custom callback to the standard WooCommerce filter woocommerce_json_search_found_customers. This filter fires whenever the plugin performs an AJAX search for customers or orders.
| Element | Explanation |
|---|---|
if( isset( $_GET['wpo_find_customer'] ) ) | Limits the customisation exclusively to the Phone Orders search context. Without this check, the filter would also run on other admin pages where the same WooCommerce search is used, potentially causing conflicts. |
add_filter( "woocommerce_json_search_found_customers", ... ) | Hooks into the filter that controls the autocomplete result set. |
foreach($customers as $key=>$data) | Loops through each result returned by the search. The $data array contains a type key that tells you whether the result is a customer account or an order. |
For orders ($data['type'] == 'order') | The code retrieves the full WC_Order object. It then builds a custom title that includes the customer’s name (using the first available from billing or shipping), a custom meta field called SavingsID, and the billing address. |
For customers ($data['type'] == 'customer') | The code retrieves the WC_Customer object. It builds a similar title using the customer’s name, a custom meta field, and the billing address. |
$customers[$key]['title'] = $title | Overwrites the default title for each result with the newly constructed string. |
The example retrieves a custom meta field with the key SavingsID. You can replace this with any meta key that exists in your database.
Step‑by‑Step Implementation
Step 1: Choose where to add the code
You have two safe options for adding custom PHP code:
- Option A (Recommended): Use the free Code Snippets plugin. Install it from Plugins → Add Newby searching for “Code Snippets”. This method allows you to add, activate, and deactivate snippets without ever editing your theme files.
- Option B: Add the code to your child theme’s
functions.phpfile. Never add custom code directly to a parent theme, because it will be lost when the theme is updated.
Step 2: Insert the snippet
- If using Code Snippets:
- If editing
functions.php:
Step 3: Test the functionality
- Go to WooCommerce → Phone Orders.
- Start typing a customer name into the Find or create a customer search bar.
- The autocomplete dropdown should now display your custom title (e.g., “John Doe (#SAVE123 – 123 Main St)”).
- Select a result and verify that the customer information loads correctly.
Customisation Options
The code is designed to be easily adapted to your specific data needs.
Replace the custom meta key
The example uses SavingsID as a custom meta key. Replace it with any key that exists for your customers or orders.
|
1 2 3 4 |
// For an order meta field get_post_meta( $order->get_id(), "_my_custom_field", true ) // For a customer user meta field get_user_meta( $customer->get_id(), "loyalty_points", true ) |
Change the title format
Modify the sprintf pattern to change how the title appears. The current format is: Name (#SavingsID – Address).
|
1 2 3 4 5 |
// Show only name and savings ID $title = sprintf( '%s (ID: %s)', $name, $savings_id ); // Add a custom prefix $title = sprintf( 'VIP: %s - %s', $name, $billing_address ); |
Add conditional formatting
You can add logic to change the title based on certain conditions. For example, highlight VIP customers.
|
1 2 3 |
$savings_id = get_user_meta( $data['id'], "SavingsID", true ); $prefix = ( $savings_id === 'VIP' ) ? '🌟 ' : ''; $title = $prefix . sprintf( ... ); |
Exclude specific results from the dropdown
If you want to remove certain customers or orders from the autocomplete results entirely, you can unset them inside the loop.
|
1 2 3 4 5 |
// Remove customers without a billing address if ( empty( $customer->get_billing_address_1() ) ) { unset( $customers[$key] ); continue; } |