The frontend WooCommerce product search, for some reason, doesn’t work for SKU values. 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!
I know for sure there is a product with SKU = “24-WG085” on my dev site, yet there is no search result. The below snippet fixes this.
PHP Snippet: Enable Product Search by SKU (including variations)
Please let me know in the comments if everything went 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.
If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!
Need Help with WooCommerce?
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!
Rodolfo Melogli
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.
I noticed the Search Products by SKU works on simple products, but it does not find the SKU of product variations. Is there a line we can add to pickup both?
Sorry. I might have had a little gremlin. I copied your code again into my snippet to replace what I had and it seems to work fine now. I should have done that before I wrote the comment.
I tried to place this code to WPCode – create snippet but it’s not working and it actually goes into 500 error. Even if I add it to Flatsome Child: functions.php .. nothing. What am I doing wrong? Thank you!
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?
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
Hi Rodolfo,
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.
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
thanks man !
You’re welcome!
I noticed the Search Products by SKU works on simple products, but it does not find the SKU of product variations. Is there a line we can add to pickup both?
Works for me on any theme. Are you using the WordPress search bar or the WooCommerce one?
Sorry. I might have had a little gremlin. I copied your code again into my snippet to replace what I had and it seems to work fine now. I should have done that before I wrote the comment.
Great!
Hi, Redolfo!
I tried to place this code to WPCode – create snippet but it’s not working and it actually goes into 500 error. Even if I add it to Flatsome Child: functions.php .. nothing. What am I doing wrong? Thank you!
Not sure, works for me. What does the error say?
Hi!
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!
I haven’t find varitations by this code too. Shows no reslut for the variation even was setted seperated sku id.
Not sure, it works for me on my test site. Try switching temporarily to Storefront theme + disable all plugins except WooCommerce and try again
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.
Hi Rodolfo,
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?