WooCommerce: Search Products By SKU

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!

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 this snippet?

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.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 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.

9 thoughts on “WooCommerce: Search Products By SKU

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

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

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

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