algoliasearch-wordpress icon indicating copy to clipboard operation
algoliasearch-wordpress copied to clipboard

Displaying hierarchical categories in autocomplete search

Open yratof opened this issue 7 years ago • 12 comments

Q: How can we show a breadcrumb of categories in the autocomplete. ie: Men > Summertime > Jackets rather than just Jackets

What did you expect to happen?

When Searching for "trousers", the results for the categories should bring back:

  • Mens Trousers
  • Womens Trousers
  • Childrens Trousers

What happened instead?

Slightly confusing because these are actually supposed to be separate categories, but algolia only brings back the lowest category:

  • Trousers
  • Trousers
  • Trousers

How can we reproduce this behaviour?

Create several categories, then give each a child of the same name. Add categories to your autocomplete search and see the list of "Trouser

Can you provide a link to a page which shows this issue?

https://fjellr-2255.rask22.raskesider.no/fjellrevenshop/

Technical info

  • WordPress version: 4.9.5
  • Algolia Search plugin version: 2.10.2

yratof avatar May 16 '18 08:05 yratof

Having the same problem, multiple categories showing up, having the same name (normal hierarchy for clothing estores men/jackets woman/jackets children/jackets). Looking for a good solution for this.

Propell1 avatar May 28 '18 06:05 Propell1

If anybody could provide a fix for this issue, that would be splendid.

rafal-sokolowski avatar May 28 '18 07:05 rafal-sokolowski

Tried to open a discord about this, but no one is responding - https://discourse.algolia.com/t/displaying-woocommerce-product-categories-with-parents/5275

yratof avatar May 28 '18 07:05 yratof

Hi everyone,

This sounds like a bug, however I don't think we've recently touched the indexing of taxonomies.

@yratof is there any chance you could send a quick email to [email protected] by providing us read access to your Algolia account? Please note that my above assumptions were based on my first understanding of your issue. https://www.algolia.com/users/edit#?tab=access-control

This will allow me to troubleshoot this.

Also, it seems that in the meantime you've disabled Algolia on the staging website, could you turn it back on so that I could observe the issue?

Thank you for your patience.

rayrutjes avatar May 28 '18 07:05 rayrutjes

Sure thing, I turned it off because I've gone live with the current working version & didn't want to eat too much into the quota. Will enable now & shoot over an email with read access

yratof avatar May 28 '18 07:05 yratof

Everything sent over & reactivated now @rayrutjes, you can see the problem when searching for jakker

yratof avatar May 28 '18 08:05 yratof

I took a look and see that: You indeed have 2 categories named jakker with different parent categories though.

By default we only display the term name.

I think the easiest here would be to index the full breadcrumb at indexing time.

You could leverage the algolia_term_record filter to override the term's name: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/indices/class-algolia-terms-index.php#L65

Implementation of the generation of the breadcrumb is left for implementation though.

Let me know how it works out for you.

rayrutjes avatar May 28 '18 10:05 rayrutjes

Actually I tried something like this, but I couldn't get it to index, perhaps you can see the problem:

     add_filter( 'algolia_term_product_cat_record', 'get_term_hierarchy', 10, 2 );

     function get_term_hierarchy( $record, $item ) {
        $tax   = $item->taxonomy;
        $terms = get_the_terms( $item->term_id, $tax );
        $ancestors = [];
        // Check we have terms
        if ( $terms && ! is_wp_error( $terms ) ) {
          $ancestors = array_reverse( get_ancestors( $terms[0]->term_id, $tax ));
          // Check if has parent
          if ( $ancestors ) {
            foreach ( $ancestors as $ancestor ) {
              $parent = get_term( $ancestor, $tax );
              // Check parent is term
              if( $parent && ! is_wp_error( $parent ) ) {
                // Add to array
                $ancestors[] = $parent->name;
              }
            }
            $record['breadcrumb_hierarchy'] = implode( ' > ', $ancestors ) . ' > ' . $terms[0]->name;
          }
        }
        return $record;
      }

This is often not getting any terms or items from the filter

yratof avatar May 28 '18 10:05 yratof

You could maybe inspire from this: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/class-algolia-utils.php#L56

Not sure to see what's wrong with your implementation.

rayrutjes avatar May 28 '18 10:05 rayrutjes

What causes me more problems is displaying this data within the autocomplete.php – There doesn't seem to be any documentation on getting custom fields into this area, and the wrong attribute breaks the whole block

yratof avatar May 28 '18 15:05 yratof

If you are not using the default name attribute, you could override that one with the breadcrumb. That way everything should work without you needing to change anything but the indexing.

Could that work in your case?

rayrutjes avatar May 28 '18 16:05 rayrutjes

I think it would work, it's not the cleanest solution, but you're right, saving the parent & the child to the name allows me to fake it. It would be a nice feature to incorporate going forward though, as this is a problem with a few of our sites currently

yratof avatar May 29 '18 12:05 yratof