GET FREE VERSION GET THE PRO VERSION

How to Search and Display WooCommerce Product Variations

Overview

WooCommerce, by default, allows store owners to create product variations, which are different versions of a product (e.g., size, color, material). However, searching for and displaying these variations in the default WooCommerce search results is not possible without customization.

In this article, we will cover:

  • What product variations are in WooCommerce.
  • Why it is important to search and display them.
  • How to enable variation search using a custom code snippet.
  • How to enable variation search using the Advanced Woo Search plugin.

By the end of this guide, you'll know how to improve your WooCommerce store's search functionality to include product variations in search results.

What Are Product Variations in WooCommerce?

WooCommerce product variations allow store owners to offer multiple options for a single product. For example, if you sell T-shirts, you might have variations based on size (S, M, L) and color (red, blue, black). Each variation can have its own price, stock level, and image.

WooCommerce product variation on frontend

WooCommerce product variation on frontend

WooCommerce product variation on backend

WooCommerce product variation on backend

Why Do You Need to Search and Display Product Variations?

Searching for product variations is important for both store owners and customers because:

  • Customers can find specific product versions more easily.
  • It improves user experience by reducing the number of steps needed to locate a desired variation.
  • It helps increase conversions by displaying the exact product customers are looking for.

However, WooCommerce’s default search does not index or display variations, making it difficult for customers to find them directly.

Default WooCommerce Search Limitations

WooCommerce’s built-in search function only looks for parent products and does not consider variations. This means that if a customer searches for a variation-specific term like “blue T-shirt” or “size M,” they won’t find relevant results unless those terms are included in the parent product’s title or description.

Default WooCommerce product search results - no product variations displayed

Default WooCommerce product search results - no product variations displayed

To improve this, we need to enable variation search using either custom code or a plugin.

Solution 1: Enable Product Variations Search with a Custom Code Snippet

The first way to enable variation search in WooCommerce is by using a custom code snippet.

This method has limited options and requires some coding knowledge.

Additionally, it may not work with certain WordPress themes or page builders.

To start displaying product variations in WooCommerce search results, use the following code snippet:

function include_product_variations_in_search($query) {
    if (!is_admin() && $query->is_main_query() && $query->is_search()) {
        $query->set('post_type', array('product', 'product_variation'));
        add_filter('posts_clauses', 'handle_product_variation_search_clauses', 10, 2);
    }
    return $query;
}
add_action('pre_get_posts', 'include_product_variations_in_search');


function handle_product_variation_search_clauses($clauses, $query) {
    global $wpdb;

    if (is_array($query->get('post_type')) && in_array('product_variation', $query->get('post_type'))) {
        $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} as variation_meta ON ({$wpdb->posts}.ID = variation_meta.post_id AND variation_meta.meta_key LIKE 'attribute_%')";
        $search_term = $query->get('s');
        if (!empty($search_term)) {
            $like = '%' . $wpdb->esc_like($search_term) . '%';
            $clauses['where'] = str_replace(
                "AND (((({$wpdb->posts}.post_title LIKE",
                "AND ((({$wpdb->posts}.post_title LIKE '$like') OR (variation_meta.meta_value LIKE '$like')) OR (({$wpdb->posts}.post_title LIKE",
                $clauses['where']
            );
        }
        $clauses['groupby'] = "{$wpdb->posts}.ID";
    }

    return $clauses;
}

function display_variation_attributes_in_title($title, $id = null) {
    if (is_search() && get_post_type($id) === 'product_variation') {
        $variation = wc_get_product($id);
        if ($variation) {
            $attributes = $variation->get_variation_attributes();
            $attr_display = array();

            foreach ($attributes as $name => $value) {
                $taxonomy = str_replace('attribute_', '', $name);

                if (taxonomy_exists($taxonomy)) {
                    $term = get_term_by('slug', $value, $taxonomy);
                    if ($term && !is_wp_error($term)) {
                        $value = $term->name;
                    }
                }

                $attr_display[] = wc_attribute_label($taxonomy) . ': ' . $value;
            }

            if (!empty($attr_display)) {
                $parent = wc_get_product($variation->get_parent_id());
                $parent_title = $parent ? $parent->get_title() : '';
                $title = $parent_title . ' - ' . implode(', ', $attr_display);
            }
        }
    }
    return $title;
}
add_filter('the_title', 'display_variation_attributes_in_title', 10, 2);

Add this snippet inside your child theme's functions.php file or use a code snippets plugin.

This code works as follows:

1. The user submits a search query using the default WooCommerce search form.
2. The code checks the search terms against the product title and content, including child products.
3. Search results are displayed, where child products (if available) inherit the parent product's image.

The variation’s title includes the main product name and variation attributes.

Default WooCommerce search results with product variations

Default WooCommerce search results with product variations

As seen above, this method allows us to search for and display product variations. But what if you need more advanced features? For example, searching within product attributes or displaying only variations while excluding parent products?

For these and many other advanced features, the best solution is the Advanced Woo Search plugin. Below, we’ll cover how to enable variation search using this plugin and explore its additional capabilities.

Solution 2: Enable Product Variations Search with Advanced Woo Search Plugin

Now, let's look at how to enable product variation search and display using the Advanced Woo Search PRO plugin.

Why Use the Advanced Woo Search PRO Plugin?

✅ Search and display parent products, child products, or both.
✅ Search within product titles, content, attributes, taxonomies, SKUs, and custom fields.
✅ Filter search results using various parameters.
✅ Live search – Display search results instantly as you type.
✅ Relevance-based sorting – Show the most relevant results first.

How to Set Up Variation Search with Advanced Woo Search PRO Plugin

Follow these steps:

1. Download, install, and activate the plugin.

2. Go to the plugin settings page -> Performance tab. Ensure the Index variations option is enabled. Additionally, check the Data to index option and enable all necessary fields.

'Index variations' option

'Index variations' option

3. Reindex the plugin table by clicking the 'Reindex table' button in the settings. This action only needs to be done once.

'Index variations' option

'Reindex table' option

4. Navigate to the search form settings -> Search Results page. Check the Variable products option to display product variations.

'Variable products' option

'Variable products' option

5. Adjust the Search In settings to include all necessary sources. For example, if you want to search within product attributes, enable the Attributes field.

Attributes field for searching

Attributes field for searching

6. Save your changes and test the search results—product variations should now be visible in both live search and search results pages.

Product variations in live search results

Product variations in live search results

Product variations in search results page

Product variations in search results page

Each product variation displays its attribute name and values. You can also search for specific variations using these attributes.

Conclusion

FeatureDefault WooCommerce SearchWooCommerce Search with Code SnippetAdvanced Woo Search Plugin
Products Search✅ Yes✅ Yes✅ Yes
Product Variations Search❌ Not supported✅ Limited support✅ Full support
Search in Product Title✅ Yes✅ Yes✅ Yes
Search in Product Content✅ Yes✅ Yes✅ Yes
Search in Product Attributes❌ No❌ No✅ Yes
Search in SKUs❌ No❌ No✅ Yes
Search in Custom Fields❌ No❌ No✅ Yes
Search in Taxonomies (Tags, Categories, etc.)❌ No❌ No✅ Yes
Live Search (Instant Results)❌ No❌ No✅ Yes
Relevance-Based Sorting❌ No❌ No✅ Yes
Supports Filtering Options❌ No❌ No✅ Yes
Works with All WordPress Themes✅ Yes⚠️ May not work with some themes✅ Yes
Requires Coding Knowledge❌ No✅ Yes❌ No
Ease of Setup✅ Simple⚠️ Requires manual implementation✅ Easy setup via UI

WooCommerce does not support product variation search by default, but you can enhance it using:

  • Custom PHP code, which requires manual implementation.
  • The Advanced Woo Search plugin, which provides an easy and powerful solution.

If you're looking for a quick and reliable way to enable variation search, Advanced Woo Search is the best option.

By implementing either method, you’ll enhance the shopping experience for your customers and boost your store's conversion rate.

Comments

Download free version

Download free version from wordpress.org repository.

Purchase pro version

Read about differences between free and pro versions here.