Hi,
You need to use the widget "Product Search" instead of "Search". https://prnt.sc/k94f7v
You want to add search by Product SKU: Currently, Woocommerce doesn't support this function.
To accomplish this I need to modify the search query and tell WordPress to also look at the _sku post meta field.
I have added a function to the functions.php file of Child Theme (the main site and staging site).
function iconic_product_search_join( $join, $query ) {
if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) {
return $join;
}
global $wpdb;
$join .= " LEFT JOIN {$wpdb->postmeta} iconic_post_meta ON {$wpdb->posts}.ID = iconic_post_meta.post_id ";
return $join;
}
add_filter( 'posts_join', 'iconic_product_search_join', 10, 2 );
function iconic_product_search_where( $where, $query ) {
if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) {
return $where;
}
global $wpdb;
$where = preg_replace(
"/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"({$wpdb->posts}.post_title LIKE $1) OR (iconic_post_meta.meta_key = '_sku' AND iconic_post_meta.meta_value LIKE $1)", $where );
return $where;
}
add_filter( 'posts_where', 'iconic_product_search_where', 10, 2 );
You can reload your site and check again.
Note: It will work fine with Woo default search function, but not sure with the custom search product in Motor Theme.
Best regards,
tinhbeng.