Search by category
-
First off, loving the plugin!
As an example: parent category called “harnesses”, with “fall arrest” “chest” “children” “positioning” and more subcategories.
When someone searches for “harness” nothing from these categories shows up.
I’m guessing because they’re not directly listed within “harness”, but only in a subcategory of it.
Is it possible that it would also show all the ones in the subcategory?EDIT: And wondering if it would also work if someone searched for “harness”, but then in french (for example).
- This topic was modified 4 years, 2 months ago by tlouwet.
-
Hello,
Looks like I found the solution for you. Please use following code snippet
add_filter( 'aws_terms_search_query', 'my_aws_terms_search_query' ); function my_aws_terms_search_query( $sql ) { $sql = str_replace( 'AND count > 0', '', $sql ); return $sql; } add_filter('aws_search_tax_results', 'my_aws_search_tax_results'); function my_aws_search_tax_results( $result_array ) { if ( isset( $result_array['product_cat'] ) ) { $new_array = array(); foreach( $result_array['product_cat'] as $key => $product_cat ) { $term_childrens = get_term_children( $product_cat['id'], 'product_cat' ); if ( $product_cat['count'] ) { $new_array[] = $product_cat; } if ( is_wp_error( $term_childrens ) || ! $term_childrens ) { continue; } foreach( $term_childrens as $term_children ) { $term = get_term( $term_children, 'product_cat' ); if ( $term != null && !is_wp_error( $term ) ) { $new_array[] = array( 'name' => $term->name, 'id' => $term->term_id, 'count' => ( $term->count > 0 ) ? $term->count : '', 'link' => get_term_link( $term ) ); } } } $result_array['product_cat'] = $new_array; } return $result_array; }
You need to add it somewhere outside the plugins folder. For example, inside functions.php file of your theme or use some plugin for adding code snippets.
Also after adding this code you will need to go to the plugin settings page and click ‘Clear cache’ button.
Regards
Added it to functions.php, next to other functions I use for other things.
I did “Clear Cache” but it doesn’t seem to change anything in the search results.
It still only shows products that are directly in a category called “Harnesses” (slug “Harnesses”) of one brand
Nothing from the others:
– category called “Harnesses” (slug “harnesses-working-rescue”) of another brand, where I need to show the child-categories products
– category also called “Harnesses” (slug “harnesses-sport”) of that same brand, where I need to show the child-categories products- This reply was modified 4 years, 2 months ago by tlouwet.
Had to test something:
I placed 1 product from a child-category of slug “harnesses-working-rescue” into the parent (slug “harnesses-working-rescue”), and that 1 product does show up when searching “harness”, but still nothing from the child-categories.And looked around a bit to see what the code leads to/does.
The code you gave shows subcategories of “harnesses”, if I enable “Archive pages -> Categories”.
But it’s the products itself I’m after, not the category listing itself ??
Thanks for the aid btw.
- This reply was modified 4 years, 2 months ago by tlouwet.
So, you need this for the products search results and not for categories archive pages search?
@mihail-barinov yep just for products search.
I just want it so that when people search “harness” they get listed all products that are within “harness” categories, and all child-categories there of.
Reason being, for some brands I have their “harnesses” split up into different child-categories, “positioning”, “fall arrest”, etc.
The products within those child-categories don’t show up when searching for “harness”.Well, in this case you can use Synonyms plugin feature. Put inside synonyms field commas separated list of parent and child categories names and than re-index plugin table.
Perhaps this will fill your needs.Regards
Some other product types are sometimes also divided under the same naming, “positioning” for example, but I could rename the child-categories to be more specific.
Totally overlooked Synonyms to be a possible solution.
Will do the trick ??Thanks for the quick replies/solution!
Sadly trying to resolve this through synonyms leads to odd results.
I changed “positioning” to “positioningsharnesses” (“positioneringsharnassen” in dutch).
I already have synonyms for “harnesses”, in dutch that would be “gordel, gordels, harnas, harnassen”.
– If I search for “harnassen” it shows up
– If I search for “gordels” it doesn’t show those, only shows the products that are directly under the category called “harnassen”Sadly I can’t make “positioneringsharnassen” a synonym of “harnassen”, because if I do the same for “zitharnassen”, searching for one will also show the other.
Was pretty much why I was wondering if showing products of sub-categories was possible, would be directly solving it, as the categories “Harnassen” hold all different types of harnesses.
I hope that makes sense.
Maybe a better example why synonyms doesn’t really do the trick:
“Hooks” is a child-category of “carabiners”.
If people search for “hooks” I want them to just see the products within the child-category “Hooks”, not the rest of the “carabiners”.
If people search for “carabiners” I want them to also see “Hooks”.EDIT: sorry for making it look complicated ^^
- This reply was modified 4 years, 2 months ago by tlouwet.
Ok, looks like I found another solution for you. Please use following code
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids', 10, 2 ); function my_aws_search_results_products_ids( $products_ids, $s ) { $data = array(); $data['s'] = $s; $data['s_nonormalize'] = $s; $data['search_terms'] = array(); $search_array = array_unique( explode( ' ', $s ) ); $search_array = AWS_Helpers::filter_stopwords( $search_array ); if ( is_array( $search_array ) && ! empty( $search_array ) ) { foreach ( $search_array as $search_term ) { $search_term = trim( $search_term ); if ( $search_term ) { $data['search_terms'][] = $search_term; } } } $tax_search = new AWS_Tax_Search( array( 'product_cat' ), $data ); $tax_search_res = $tax_search->get_results(); $all_child_terms = array(); if ( $tax_search_res && isset( $tax_search_res['product_cat'] ) && ! empty( $tax_search_res['product_cat'] ) ) { foreach( $tax_search_res['product_cat'] as $tax ) { $child = get_term_children( $tax['id'], 'product_cat' ); $all_child_terms = array_merge( $all_child_terms, $child ); } } if ( ! empty( $all_child_terms ) ) { $args = array( 'posts_per_page' => -1, 'fields' => 'ids', 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'suppress_filters' => true, 'no_found_rows' => 1, 'orderby' => 'ID', 'order' => 'DESC', 'lang' => '', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'ID', 'terms' => $all_child_terms, 'operator' => 'IN', ) ) ); $posts = get_posts( $args ); if ( $posts ) { $products_ids = array_merge( $products_ids, $posts ); } } return $products_ids; }
Ooooh so close!
if ( $tax_search_res && isset( $tax_search_res['product_cat'] ) && ! empty( $tax_search_res['product_cat'] ) ) { foreach( $tax_search_res['product_cat'] as $tax ) { $child = get_term_children( $tax['id'], 'product_cat' ); $all_child_terms = array_merge( $all_child_terms, $child ); /*if ($tax['id'] == '348') { return array(725); }*/ } }
For test purposes, I added the part in between /* */ to check if it calls id 348 (the category holding all the child-categories), with a visual representation of product id ‘725’.
Category id ‘348’ doesn’t directly hold products, only his child-categories do.
Now, if I pull one of the products from a child-category into the id ‘348’ category, it all works.
But once I change it back, child-categories doesn’t show up.So I think the issue is that $tax_search or $tax_search_res ignores “empty” categories?
- This reply was modified 4 years, 2 months ago by tlouwet.
After further inspection, I think (I’m not 100% experienced with php, but learning) it’s because of:
advanced-woo-search/includes/class-aws-tax-search.php
~line 123$sql = " SELECT distinct($wpdb->terms.name), $wpdb->terms.term_id, $wpdb->term_taxonomy.taxonomy, $wpdb->term_taxonomy.count, {$relevance_query} as relevance FROM $wpdb->terms , $wpdb->term_taxonomy WHERE 1 = 1 {$search_query} AND $wpdb->term_taxonomy.taxonomy IN ( {$taxonomies_names} ) AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id AND count > 0 {$excludes} GROUP BY term_id ORDER BY relevance DESC, term_id DESC LIMIT 0, {$terms_number}";
where it states “AND count > 0”
If I remove that single line, it works!
Do I do something wrong by removing that line?And is there a filter & function I could add to my functions.php for the $sql to be the same but without “AND count > 0”?
Yes, you are right about that. You can remove
AND count > 0
by using following snippetadd_filter( 'aws_terms_search_query', 'my_aws_terms_search_query' ); function my_aws_terms_search_query( $sql ) { $sql = str_replace( 'AND count > 0', '', $sql ); return $sql; }
@mihail-barinov Oh didn’t expect filters to be that straight forward.
You’re a life saver :).
Thanks a lot for the solution and support!As the company also proposed, I will forward them the Advanced Woo Search PRO purchase.
They were amazed with the direct and swift support.For future searchers:
Add following to functions.phpadd_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids', 10, 2 ); function my_aws_search_results_products_ids( $products_ids, $s ) { $data = array(); $data['s'] = $s; $data['s_nonormalize'] = $s; $data['search_terms'] = array(); $search_array = array_unique( explode( ' ', $s ) ); $search_array = AWS_Helpers::filter_stopwords( $search_array ); if ( is_array( $search_array ) && ! empty( $search_array ) ) { foreach ( $search_array as $search_term ) { $search_term = trim( $search_term ); if ( $search_term ) { $data['search_terms'][] = $search_term; } } } $tax_search = new AWS_Tax_Search( array( 'product_cat' ), $data ); $tax_search_res = $tax_search->get_results(); $all_child_terms = array(); if ( $tax_search_res && isset( $tax_search_res['product_cat'] ) && ! empty( $tax_search_res['product_cat'] ) ) { foreach( $tax_search_res['product_cat'] as $tax ) { $child = get_term_children( $tax['id'], 'product_cat' ); $all_child_terms = array_merge( $all_child_terms, $child ); } } if ( ! empty( $all_child_terms ) ) { $args = array( 'posts_per_page' => -1, 'fields' => 'ids', 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'suppress_filters' => true, 'no_found_rows' => 1, 'orderby' => 'ID', 'order' => 'DESC', 'lang' => '', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'ID', 'terms' => $all_child_terms, 'operator' => 'IN', ) ) ); $posts = get_posts( $args ); if ( $posts ) { $products_ids = array_unique(array_merge( $products_ids, $posts )); } } return $products_ids; } add_filter( 'aws_terms_search_query', 'my_aws_terms_search_query' ); function my_aws_terms_search_query( $sql ) { $sql = str_replace( 'AND count > 0', '', $sql ); return $sql; }
- This reply was modified 4 years, 2 months ago by tlouwet.
- The topic ‘Search by category’ is closed to new replies.