The frontend WooCommerce product search, for some reason, doesn’t work for SKU numbers. If it does, then your theme developers were smart enough to include this in their code, because this is a big problem especially for B2B stores.
Today, we’ll study how to alter the product search query, as well as the wc_get_product_id_by_sku() function, which is super helpful to determine the product ID for a given SKU. Enjoy!
PHP Snippet: Enable Product Search by SKU (including variations)
You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
Does this snippet (still) work?
Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
9 thoughts on “WooCommerce: Search Products By SKU”
Great addition, works well.
However, it does not seem to find variations. I did see your Admin variant of this script does (was appended later), is it possible for this query as well, do you think?
Try with the latest version please and let me know!
HI Rudolfo, Previously I posted that this snippet works without any problem, but I found that the search does not find variables. It will find the parent, but if I search for the SKU of the variable, it will not find it. Thanks
Fixed – take a look please!
I used the “Search Products by SKU” snippet exactly as it was written and it worked instantly. Thanks. That’s a keeper.
This code is just what I was looking for, unfortunately, doesn’t seem to be working on my b2b site. Do I need to change something from the code?
Thanks for all your work!
Hi Hamsa, this works on my development site. It should return the only product that has the specific SKU you enter in the search box, plus any other products that may have that string in the title or product description
Thanks for your answer! And I don’t have to change anything on that code, right? I’m playing around with it, and search options on woocommerce adjustments with out luck.
No problem. Are you experiencing problems with variations by any chance?