Make the WooCommerce Admin Product Search Find Variations Too
The product search dropdown that WooCommerce uses in the admin (the one you see when adding products to an order, building a coupon, or setting up upsells) doesn’t find variations by name. If you have a parent product called “T-Shirt” with variations “T-Shirt – Small”, “T-Shirt – Medium”, “T-Shirt – Large”, searching for “Medium” returns nothing. You can only search for the parent.
This becomes painful when your variations are the meaningful unit: shirts with different colors, courses with different cohorts, subscription plans with different tiers. Staff have to find the parent first, then mentally translate to the right variation. The snippet below extends the admin search to also match variation titles, so typing any part of a variation name finds it directly.
/**
* Extend the WooCommerce admin product search to match variation names.
*
* Hooks into the JSON search results that power dropdowns in admin pages
* (adding products to orders, configuring coupons, upsells/cross-sells, etc).
*/
add_filter('woocommerce_json_search_found_products', 'carticy_admin_search_include_variations', 10, 1);
function carticy_admin_search_include_variations($products)
{
// Only run during the relevant admin AJAX call
if (!wp_doing_ajax() || !is_admin()) {
return $products;
}
$action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : '';
if ($action !== 'woocommerce_json_search_products_and_variations') {
return $products;
}
if (empty($_GET['term'])) {
return $products;
}
$term = wc_clean(wp_unslash($_GET['term']));
global $wpdb;
$like = '%' . $wpdb->esc_like($term) . '%';
$results = $wpdb->get_results($wpdb->prepare(
"SELECT p.ID
FROM {$wpdb->posts} p
WHERE p.post_type IN ('product', 'product_variation')
AND p.post_title LIKE %s
AND p.post_status IN ('publish', 'private')
GROUP BY p.ID
LIMIT 50",
$like
));
foreach ($results as $row) {
$product = wc_get_product($row->ID);
if (!$product || !$product->is_purchasable()) {
continue;
}
$products[$row->ID] = rawurldecode(wp_strip_all_tags($product->get_formatted_name()));
}
return $products;
}
How the Native Search Works
The admin uses an AJAX action called woocommerce_json_search_products_and_variations. Despite the name, the default implementation only really searches the parent product’s title and SKU. Variations have their own post records of type product_variation, but their titles (which include the parent name plus attribute values) aren’t part of the standard search.
The snippet runs a small targeted query against wp_posts looking for any product or variation whose title matches the search term. It then loads each match through wc_get_product(), validates it’s purchasable, and adds it to the results array using get_formatted_name() which returns the human-readable form (e.g. “T-Shirt – Medium (#1234)”).
Why the Action Check Matters
WooCommerce uses several different JSON search actions: customers, downloadable products, products only (no variations), products and variations. The woocommerce_json_search_found_products filter fires for all of them. Without the action check, you’d be injecting variation results into dropdowns where they don’t belong, like the “only show parent products” search used by some plugins. The strict check keeps the change scoped to the dropdown you actually want to fix.
Performance
The query is bounded with LIMIT 50 so it stays fast even on large catalogs. $wpdb->esc_like() handles the LIKE-pattern escaping properly. The post_title column is indexed, so a LIKE with a leading wildcard isn’t ideal but is still acceptable for catalogs in the low thousands. If you have tens of thousands of variations and want better search relevance, consider a dedicated search plugin or an indexing layer, but this snippet handles most stores fine.
Verification
- Add the snippet to your child theme’s
functions.phpor a custom plugin - Make sure you have a variable product with named variations (e.g. “T-Shirt – Medium”)
- Open any order in the WooCommerce admin and click Add item(s)
- Search for the variation by attribute value (“Medium”)
- Confirm the variation appears in the results dropdown with its full name and is selectable
Need Help?
Learn how to add custom code to WordPress or reach out for custom development help.
About the Author
More Code Snippets
-
Reorder the Columns on the WooCommerce Orders List
Put your WooCommerce orders list columns in whatever order works…
-
Set the Default Country on WooCommerce Checkout
Pre-fill the WooCommerce checkout country (and optionally state) dropdown with…
-
Bulk Delete Expired Unused WooCommerce Coupons (Batched, Safe)
One-time bulk cleanup utility for stores with thousands of expired,…