How to Allow Users to Filter Orders by Searching Order Notes in WooCommerce

When managing a busy WooCommerce store, order notes are invaluable for tracking special instructions, internal communications, or shipping details. However, by default, the WooCommerce order search functionality only looks at standard fields like name, email, and order ID. This can make it incredibly frustrating when you need to find an order based on a specific note but can’t remember the customer’s name.

Since WooCommerce stores order notes as standard comments in the WordPress database, we can hook into the woocommerce_shop_order_search_results filter to extend the search query. By doing a quick lookup in the comments table, we can identify orders that contain our search term in their notes and include them in the results.

Key Code for Searching Order Notes in WooCommerce

Add the following code to your theme’s functions.php file or a custom plugin. This snippet performs a raw SQL query to find order_note entries that match the current search term and merges those IDs into the main order search results.

/**
 * Extend WooCommerce order search to include order notes.
 */
add_filter('woocommerce_shop_order_search_results', function ($order_ids, $term, $search_fields)
{
    global $wpdb;

    // Search for matches in the comments table
    $comment_query = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT comment_post_ID
             FROM {$wpdb->prefix}comments as comment_ids
             WHERE comment_content LIKE %s
             AND comment_type LIKE %s",
            '%' . $wpdb->esc_like(wc_clean($term)) . '%',
            'order_note'
        )
    );

    // Extract the post IDs (Order IDs) from the query results
    $comment_posts_id = wp_list_pluck($comment_query, 'comment_post_ID');

    // Merge matching Order IDs with the original search results
    if ( ! empty($comment_posts_id)) {
        $order_ids = array_merge($order_ids, $comment_posts_id);
    }

    return $order_ids;
}, 11, 3);

Going Further: Searching Other Custom Data

The woocommerce_shop_order_search_results filter is incredibly flexible. It provides three parameters that you can use to customize exactly how orders are filtered:

  • $order_ids: The array of order IDs identified by previous search filters. If your custom logic has no matches, simply return this original array.
  • $term: The actual search keyword entered by the user.
  • $search_fields: The specific metadata fields being searched. Usually, these are the default custom fields associated with an order.

If you want to modify which metadata fields are included in the default search (before the ID-based filtering above), you can also use the woocommerce_shop_order_search_fields filter. By combining these hooks, you can make the WooCommerce order dashboard much more powerful and tailored to your specific administrative workflow.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *