
Tagged: ajax search, SKU, woocommerce
-
AuthorPosts
-
July 30, 2014 at 6:45 am #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,
AdaAugust 1, 2014 at 5:59 am #298600Hi 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!
JosueFebruary 5, 2016 at 7:39 pm #578707Hi, 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?
February 6, 2016 at 1:55 am #578897Hey!
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,
JosueJuly 20, 2018 at 10:39 am #987765come 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 3 years, 10 months ago by
Raphael.
July 23, 2018 at 3:53 pm #988741Hi hunter74,
Thank you for sharing! :)
If you need further assistance please let us know.
Best regards,
VictoriaJuly 31, 2019 at 8:10 pm #1123641I’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!
KevinAugust 3, 2019 at 6:49 pm #1124398Hi Kevin,
You can look at the code above, it can point you in the right direction.
Best regards,
VictoriaAugust 12, 2019 at 8:08 pm #1126960This 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,
KevinAugust 12, 2019 at 9:40 pm #1127002I 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!
-
This reply was modified 2 years, 9 months ago by
kevinmcgillivray.
August 15, 2019 at 8:49 pm #1128067Hi,
Thank you for sharing and we are happy you got it shorted out!
Best regards,
BasilisNovember 9, 2019 at 3:46 am #1155141Thanks for sharing this fix. Woocommerce have added search by SKU to core as of 3.6.x however Enfold breaks it for some reason.
November 13, 2019 at 2:55 am #1156245 -
This reply was modified 3 years, 10 months ago by
-
AuthorPosts
The topic ‘Product Search fail to find products by SKU’ is closed to new replies.