Person using laptop on a sofa]

Allow WooCommerce SKUs in search queries

CommerceCategory
3 min read
Beka Rice

Currently, you can only search for WooCommerce SKUs in the admin of your shop (the “Products” list), and this search is restricted to only parent product SKUs, not variation SKUs. In WooCommerce 2.5, you’ll be able to search your admin by SKU for parent product SKUs and variation SKUs.

However, this search functionality isn’t available on the frontend of your site; customers cannot search for product SKUs to return a list of products. If customers search for all or part of a SKU, and the search query isn’t contained anywhere else on the site, they’ll get no results.

No products were found - Woocommerce

We can make some changes that will let customers search for products by SKU on your site instead using search plugins that modify the default WordPress search.

WooCommerce SKU search for simple products

If you have mostly simple products, then the easiest way to allow customers to search by SKU is to install the free Relevanssi plugin. This will replace you WordPress and WooCommerce product searches with the Relevanssi search, and will allow you to include product SKUs in the search index.

First, go to Settings > Relevanssi and configure your desired settings. The most important one to change right now is to include the “product” post type in your site’s search results.

Indexing options in Woocommerce
Enable products in search results

Once you’ve done that, you’ll want to add the custom field for your product SKU to your search index. Scroll down to “Custom Fields to index” and add _sku to this field (the underscore matters). This will tell Relevanssi to include SKUs in the search index, and lets customers enter the SKU as a search query.

Custom field to index
Add SKU to search index

Now when a customer searches for a string that’s part of your simple / parent product’s SKU, the product will be returned as a result:

Product search

However, variation SKUs (if different from the parent product’s SKU), will not be searchable. Product variations are stored with some data separated from the parent product, and the SKU is one of these pieces of data. Therefore, if the variation’s SKU is different, searching for the variation SKU will not return the product in search results.

WooCommerce SKU search for variable product SKUs

If you need to have variation SKUs included in your search index as well as simple / parent product SKUs, you’ll need to use SearchWP ($49+) instead of Relevanssi. The WooCommerce add-on / integration for it (included with any license) will let you include products within your search.

However, you can also add a bit of custom code to store variation SKUs on the parent product, as outlined by the plugin documentation. You can then include this custom field in your search, and variation SKUs will also be included in your search index.