ResolvedSearch by SKU
- This topic has 4 replies, 2 voices, and was last updated 2 years, 2 months ago by
egorp.
- AuthorPosts
- September 9, 2021 at 6:15 am #31592
egorp
ParticipantIs it possible for the search engine to find the products when visitors type the product’s SKU code?
Other search plugins allow it, but the one that comes with the template does not…
Thank you.
September 9, 2021 at 1:24 pm #31603Althemist
KeymasterI am afraid no. It’s not theme related. WooCommerce just doesn’t have such option.
There are some plugins for this, but none of them work really fine.
September 10, 2021 at 7:32 pm #31640egorp
ParticipantThanks for your answer, I understand that it is not a functionality of the theme, but I can tell you that I have had the opportunity to create other websites that already have the option to search by SKU included, and they work perfect !.
I just wanted to know if maybe there was an option within the configuration that would allow it, and as a recommendation it would be great if they took it into account for a next update.
Thank you again.
September 10, 2021 at 8:07 pm #31641egorp
ParticipantIn fact, I just found a solution to add a snippet to the functions, and it worked with the search engine that comes by default with the template:
——
/* Add sku to product search */
function az_pre_get_posts( $query ) {
// conditions – change the post type clause if you’re not searching woocommerce or ‘product’ post type
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || ! get_query_var(‘post_type’)==’product’ ){
return;
}
add_filter(‘posts_join’, ‘az_search_join’ );
add_filter(‘posts_where’, ‘az_search_where’ );
add_filter(‘posts_groupby’, ‘az_search_groupby’ );}
add_action( ‘pre_get_posts’, ‘az_pre_get_posts’ );function az_search_join( $join ){
global $wpdb;
$join .= ” LEFT JOIN $wpdb->postmeta gm ON (” .
$wpdb->posts . “.ID = gm.post_id AND gm.meta_key=’_sku’)”; // change to your meta key if not wooreturn $join;
}function az_search_where( $where ){
global $wpdb;
$where = preg_replace(
“/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\’]+\’)\s*\)/”,
“({$wpdb->posts}.post_title LIKE $1) OR (gm.meta_value LIKE $1)”, $where );
return $where;
}
/* grouping by id to make sure no dupes */
function az_search_groupby( $groupby ){
global $wpdb;
$mygroupby = “{$wpdb->posts}.ID”;
if( preg_match( “/$mygroupby/”, $groupby )) {
// grouping we need is already there
return $groupby;
}
if( !strlen(trim($groupby))) {
// groupby was empty, use ours
return $mygroupby;
}
// wasn’t empty, append ours
return $groupby . “, ” . $mygroupby;
}
——Activating the option “Search only in Products” in Theme Options.
The only thing that seemed unfavorable to usability is that the user must give enter for the results to appear, because the thumbnails do not appear as when searching by name …
If anyone knows how to edit the snnipet or have knowledge it would be helpful.
September 10, 2021 at 9:11 pm #31642egorp
ParticipantFound the solution!!
——
function search_by_sku( $search, &$query_vars ) {
global $wpdb;
if(isset($query_vars->query[‘s’]) && !empty($query_vars->query[‘s’])){
$args = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’,
‘meta_query’ => array(
array(
‘key’ => ‘_sku’,
‘value’ => $query_vars->query[‘s’],
‘compare’ => ‘LIKE’
)
)
);
$posts = get_posts($args);
if(empty($posts)) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if(sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( ‘AND (((‘, “AND ((({$wpdb->posts}.ID IN (” . implode( ‘,’, $get_post_ids ) . “)) OR (“, $search);
}
}
return $search;}
add_filter( ‘posts_search’, ‘search_by_sku’, 999, 2 );
—-Thanks althemist for your help!
- AuthorPosts
You must be logged in and have valid license to reply to this topic.