WooCommerce: Search Products By SKU (Frontend)

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)

 * @snippet       Also Search by SKU @ Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/

add_filter( 'posts_search', 'bbloomer_product_search_by_sku', 9999, 2 );
function bbloomer_product_search_by_sku( $search, $wp_query ) {
	global $wpdb;
	if ( is_admin() || ! is_search() || ! isset( $wp_query->query_vars['s'] ) || ( ! is_array( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== "product" ) || ( is_array( $wp_query->query_vars['post_type'] ) && ! in_array( "product", $wp_query->query_vars['post_type'] ) ) ) return $search;	
	$product_id = wc_get_product_id_by_sku( $wp_query->query_vars['s'] );
   if ( ! $product_id ) return $search;
   $product = wc_get_product( $product_id );
	if ( $product->is_type( 'variation' ) ) {
		$product_id = $product->get_parent_id();
	$search = str_replace( 'AND (((', "AND (({$wpdb->posts}.ID IN (" . $product_id . ")) OR ((", $search );	
	return $search;   

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its 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 guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

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.

19 thoughts on “WooCommerce: Search Products By SKU (Frontend)

  1. thanks man !

  2. 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?

    1. Works for me on any theme. Are you using the WordPress search bar or the WooCommerce one?

      1. 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.

  3. 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!

    1. Not sure, works for me. What does the error say?

  4. 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?

    1. Try with the latest version please and let me know!

      1. I haven’t find varitations by this code too. Shows no reslut for the variation even was setted seperated sku id.

        1. Not sure, it works for me on my test site. Try switching temporarily to Storefront theme + disable all plugins except WooCommerce and try again

  5. 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

    1. Fixed – take a look please!

  6. I used the “Search Products by SKU” snippet exactly as it was written and it worked instantly. Thanks. That’s a keeper.

  7. 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!

    1. 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

      1. 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.

        1. No problem. Are you experiencing problems with variations by any chance?

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 :)

Your email address will not be published. Required fields are marked *