Phone Orders For WooCommerce

Search products by custom field “_barcode”

Many stores use barcodes as unique product identifiers. The Phone Orders product search does not look at custom fields by default, so entering a barcode returns no results. This guide provides a code snippet that extends the search to include the _barcode custom field. With this customisation, your agents can scan or type a barcode and instantly add the corresponding product to the order.


The Complete Code Snippet

Copy and paste the following PHP code into your child theme’s functions.php file or via the Code Snippets plugin.

How the Code Works

The snippet hooks into WordPress’s main posts_where filter, which modifies the WHERE clause of any product query. It checks for a special flag that the Phone Orders plugin sets when an agent performs a search.

ElementExplanation
add_filter( "posts_where", ... )Runs every time WordPress queries for posts (including products). The $where parameter contains the existing WHERE clause of the SQL statement.
$query->query_vars['OR_sku_filter']A custom query variable that the Phone Orders plugin sets when an agent types a search term. The variable holds the exact text the agent entered.
if ( isset( $query->query_vars['OR_sku_filter'] ) )Confirms that the current query is a Phone Orders product search. Without this check, the filter would run on all product queries, including the shop page and category archives, potentially causing performance issues.
$wpdb->esc_like( $query->query_vars['OR_sku_filter'] )Escapes any special SQL characters in the search term. This prevents SQL injection and ensures the term is treated as a literal string.
$like = "%" . ... . "%";Wraps the escaped term in wildcard characters (%). The condition matches any barcode that contains the search term anywhere (not necessarily the exact barcode).
SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_barcode' AND meta_value LIKE '...'A subquery that finds all product IDs whose _barcode meta field contains the search term.
$where .= " OR ({$wpdb->posts}.ID IN ( ... ))"Appends the barcode condition to the existing WHERE clause. The search now returns products that match the original criteria (title, SKU, etc.) orhave a matching barcode.

Important: The code uses the meta key _barcode. If your store uses a different meta key (e.g., barcodecustom_barcode), replace _barcode with your key.


Step‑by‑Step Implementation

Step 1: Choose where to add the code

  • Option A (Recommended): Use the free Code Snippets plugin. Install it from Plugins → Add Newby searching for “Code Snippets”. This method is safe and allows you to activate or deactivate the snippet without touching your theme files.
  • Option B: Add the code to your child theme’s functions.php file. Navigate to Appearance → Theme File Editor, open functions.php, paste the code at the bottom, and save. Never add custom code directly to a parent theme.

Step 2: Insert the snippet

  • If using Code Snippets:

Step 3: Test the functionality

  1. Ensure at least one product in your catalogue has a value stored in its _barcode meta field.
  2. Open a phone order (WooCommerce → Phone Orders).
  3. In the product search field, type the barcode (or part of it).
  4. The product should appear in the search results.

Customisation Options

You can easily adapt the code to search different custom fields or to change the matching behaviour.

Use a different meta key – Replace _barcode with your custom key.WHERE meta_key = 'my_barcode_field'

Search multiple meta keys – Add more subqueries with OR operators.

Exact match only (no wildcards) – Remove the % characters from the $like variable.$like = $wpdb->esc_like( $query->query_vars['OR_sku_filter'] );

Add the barcode condition to the main query without using OR_sku_filter – You can apply the filter to all product queries, but this may cause performance issues. Use the conditional check as shown in the original snippet.

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