Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #297588

    Hi,
    We’ve activated “Product Search” function in theme options, however the search function fail to find any products by their SKU, we are wondering what’s the work around for this?
    Cheers,
    Ada

    #298600

    Hi Ada!

    As far as i know WooCommerce doesn’t have this feature by default, try using this plugin: https://wordpress.org/plugins/search-by-sku-for-woocommerce/

    Cheers!
    Josue

    #578707

    Hi, there. I tested this plugin and it works fine with the regular search results page (if you press Enter in the search box) but the ajax results preview doesn’t show the products that are found. Any idea why not?

    #578897

    Hey!

    Because it probably requires the theme Ajax search functions to be modified in order to get it working, unfortunately that’s beyond our support scope (third-party plugin). One thing you can do is disable the Ajax search and use the normal WordPress search, you can do that by adding the following to your theme / child theme functions.php:

    add_filter('avf_frontend_search_form_param', function($params) {
      $params['ajax_disable'] = true;
      return $params;
    }, 10, 1);
    

    Regards,
    Josue

    #987765

    come on, this is maybe not a big deal to insert the SKU in the search filter?!

    With this awesome script the standard search will search also Custom fields like SKU) Just add it to the functions.php

    /**  __________________________________   ENHANCE THE STANDARD SEARCH 
     * Join posts and postmeta tables
     * 					 This one hels to include the SKU in tzhe standard search
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
     */
    function cf_search_join( $join ) {
        global $wpdb;
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    /**
     * Modify the search query with posts_where
     *
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
     */
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
        if ( is_search() ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
        }
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    /**
     * Prevent duplicates
     *
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
     */
    function cf_search_distinct( $where ) {
        global $wpdb;
        if ( is_search() ) {
            return "DISTINCT";
        }
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
    /* END   Search includes SKU  */
    
    • This reply was modified 1 year, 3 months ago by  hunter74.
    #988741

    Hi hunter74,

    Thank you for sharing! :)

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #1123641

    I’d like to look into modifying the ajax search to support product SKUs. I understand that’s outside the scope of normal support but can you give me a nudge in the right direction on where to start?

    Thanks!
    Kevin

    #1124398

    Hi Kevin,

    You can look at the code above, it can point you in the right direction.

    Best regards,
    Victoria

    #1126960

    This files above make sense from a filtering standpoint but don’t seem to impact the Ajax search functionality in any way I can see. What I’m asking is if you can point me in the direction of what files are related to the Ajax search.

    Thanks,
    Kevin

    #1127002

    I figured this out!!! Woo hoo!!!! Or perhaps I should say, I hacked together a solution that seems to work. I don’t know if this is the best solution or if it would work in all situations. Or that it didn’t break something I’m not aware of yet…

    For anyone who is interested, this is related to the functions-enfold.php file and the avia_ajax_search() function. The comments in this function says that it “hook[s] into wordpress ajax function to catch any ajax requests”. This function is set up to use get_posts() to get the search results when the call is made to it via WP’s admin-ajax.php file. In fact, the function contains the ability to switch out and use a custom function instead of get_posts. That might be a better way to accomplish what I’m doing here but I don’t know honestly.

    At any rate, all I did was change the filters provided by @hunter74 to also check if $_REQUEST contains the parameter “s”, This would indicate a search term was being requested. If it does, the meta values are checked for the search term and since one of the meta values is WooCommerce SKU, those records would be returned if any exist.

    So this is the revised code:

    /**  __________________________________   ENHANCE THE STANDARD SEARCH 
     * Join posts and postmeta tables
     * 					 This one hels to include the SKU in tzhe standard search
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
     */
    function cf_search_join( $join ) {
        global $wpdb;
        if ( is_search()  || !empty($_REQUEST['s'] )) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
        return $join;
    }
    add_filter('posts_join', 'cf_search_join' );
    
    /**
     * Modify the search query with posts_where
     *
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
     */
    function cf_search_where( $where ) {
        global $pagenow, $wpdb;
        if ( is_search() || !empty($_REQUEST['s'] ) ) {
            $where = preg_replace(
                "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
        }
        return $where;
    }
    add_filter( 'posts_where', 'cf_search_where' );
    
    /**
     * Prevent duplicates
     *
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
     */
    function cf_search_distinct( $where ) {
        global $wpdb;
        if ( is_search() || !empty($_REQUEST['s'] ) ) {
            return "DISTINCT";
        }
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
    /* END   Search includes SKU  */

    If anyone out there has a better (possibly more secure) solution, I’m definitely curious to hear about this. And, honestly, I think this type of feature should be added to the Enfold core or perhaps a framework for adding fields to search criteria should be defined.

    In any case, I love Enfold and really appreciate what a great theme the team has developed!

    #1128067

    Hi,

    Thank you for sharing and we are happy you got it shorted out!

    Best regards,
    Basilis

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

You must be logged in to reply to this topic.