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!

PHP Snippet: Enable Product Search by SKU (including variations)
/**
* @snippet Also Search by SKU @ Shop
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
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;
}
Wow!
Worked like a charm. I needed not exact sku match but if sku contained the search term. Just a few chnages and got it working. Came after consulting chatgpt and many other posts.
Thanks a lot. (:
Excellent!
Hi
your code works perfect!!!
I am wondering if this could also work for barcode field.
I tried by replacing sku with metaname of my barcode field, but it doesn’t seem to work.
Do you have any idea?
Hello Nikos, this snippet features the wc_get_product_id_by_sku() function, which only works for SKUs. Using a custom field would require additional custom code. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
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?