Phone Orders For WooCommerce

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

Note: The code runs only when the wpo_find_customer parameter 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.

ElementExplanation
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'] = $titleOverwrites 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.php file. 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

  1. Go to WooCommerce → Phone Orders.
  2. Start typing a customer name into the Find or create a customer search bar.
  3. The autocomplete dropdown should now display your custom title (e.g., “John Doe (#SAVE123 – 123 Main St)”).
  4. 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.

Change the title format

Modify the sprintf pattern to change how the title appears. The current format is: Name (#SavingsID – Address).

Add conditional formatting

You can add logic to change the title based on certain conditions. For example, highlight VIP customers.

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.

Have questions? Please submit a support request. We're always happy to help!

Phone Orders
for WooCommerce
PRO

From $120
Buy
✓ 30-day money-back guarantee