ResolvedSearch by SKU

Tagged: ,

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #31592
    egorp
    Participant

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

    #31603
    Althemist
    Keymaster

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

    #31640
    egorp
    Participant

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

    #31641
    egorp
    Participant

    In 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 Woocommerce Search

    ——
    /* 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 woo

    return $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.

    #31642
    egorp
    Participant

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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in and have valid license to reply to this topic.

License required for any item belonging to this account
AlThemist

AlThemist

sales 16324, followers 728
Login and Registration Log in · Register