Add Product SKU to the WooCommerce Search

The default product search is quite basic in WooCommerce, and unless you’re using a plugin like SearchWP to enhance it, your customers may not be finding the products they want. One simple enhancement you can make is to add the product SKU as a field that is included when users perform a search.

To accomplish this we need to modify the search query and tell WordPress to also look at the _sku post meta field. Inspired by “Search WordPress by Custom Fields without a Plugin” by Adam Balée, I have prepared the code for you below. You can add this code to your website using the Code Snippet plugin, or your own preferred method.

Note: If you search for a specific SKU that doesn’t match anything else, WooCommerce will automatically redirect to that specific product page. This is true for any search that returns only a single product.

If you’re using our WooCommerce Show Single Variations plugin, this will also enable searching for their SKU codes.

Leave a Reply

26 Comments on "Add Product SKU to the WooCommerce Search"

avatar
  Subscribe  
newest oldest most voted
Notify of
stacy
Guest

Hi James,

Thank you for the informative posted. I want to be able to search for the product without having to add the dash (-) or the spacing. For example, my product sku 25-467 GCA and i want to be able to just type in the search box: 25467GCA and the product appears. Any help would be appreciated.

Stacy

Wesley van Wijngaarden
Guest
Wesley van Wijngaarden

Hi,
Great script. But in backend when searching for a product gives one SKU code way too much results. 1 unique code give at least 19 results with all the same code.

Humberto Albarrán
Guest

My friend, your post is the solution I was looking for. But, I dont get where this code is going to be written.
Do I have to edit woocommerce-core-functions.php? Thanks in advance.

Punkie
Guest

Thanks for your great code snippet! I will include it in all my woo projects now. You rock!

Dave Schroeder
Guest

This seems to break the Recently Viewed Products widget on search pages. It only shows the last product shown for all of them. Anyone have a fix for this?

Abhishek Agrawal
Guest

Hey Dave! did you find any fix for it? Thanks!

Mansoor Firoozeh
Guest

Hi Dear James, tnx for sharing , How we can use it in the back-end? for example we want to search a meta_value (on the meta_key) of a product in the “Woocommerce Orders page” at the back-end.
Please help me to search through a meta_key to check the meta_vaule.
woocommerce function to search is : “woocommerce_shop_order_search_fields”

please help me to do this.
so much appreciated.
Mansoor

Abhishek Agrawal
Guest

Hey James! nice piece of code.

Can you pls suggest how to resolve the Recently Viewed section wherein last product is repeating several times after adding the snippet. Thanks much 🙂

Fei
Guest

Hi,
I copied the code you provided in a code snippet but after activated it, it doesn’t produce the wanted results..
Can you check my website?
https://amavisse-wholesale….

PS: I’m using wordpress 4.9 and woocommerce 3.2.5

Sarah
Guest

Hi James!

I’ve tried adding the code but it doesn’t seem to be working. Could you check our sites to see if it’s working for you??

http://www.allthingsa2z.com or http://www.vikingwholesalex.com

Björn
Guest
James, you really made my day. Big thank you for sharing your code.After searching for hours and being guided to the wrong directions all the time (SEO or use of plugin) I was about to give up. Then I had the idea to lookup SKU search and found your blog. Originally I wanted to improve my Woocommerce search to be able to lookup meta tags, but wasn’t able to find anything working.After testing your code I can confirm it is working and doing its job just excellent!If I want to add meta tag search now, am I right I need… Read more »
Damir
Guest

Hi James,

Code works great, thank you.
I have 3 questions:

1. how to add another meta key to the query, to search another meta besides SKU
2. Then is it possible to add that functionality to backend, SKU search already works out of box but need other meta
3. When I use woocommerce search widget it takes me directly to product page not the search results page

Thank you very much.

dummy
Guest

Hi Sir,

I want to product search by multiple sku in frontend please help me to solve this ASAP.

Jan
Guest

Hey James,
many thanks for posting this.
Just a short question: I understand this works with the standard product search widget. As I’m trying to get this working for the standard general wordpress search I had to remove the ! is_woocommerce statement to show me the results. Then it works for SKUs (1 result). Just a normal text search gives me way too many results again. Any idea?
Have a nice day and keep it up!