Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Displaying hierarchical categories in autocomplete search #759

Open
yratof opened this issue May 16, 2018 · 12 comments
Open

Displaying hierarchical categories in autocomplete search #759

yratof opened this issue May 16, 2018 · 12 comments

Comments

@yratof
Copy link

yratof commented May 16, 2018

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
@Propell1
Copy link

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.

@rafal-sokolowski
Copy link

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

@yratof
Copy link
Author

yratof commented May 28, 2018

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

@rayrutjes
Copy link
Member

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 support@algolia.com 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.

@yratof
Copy link
Author

yratof commented May 28, 2018

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
Copy link
Author

yratof commented May 28, 2018

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

@rayrutjes
Copy link
Member

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.

@yratof
Copy link
Author

yratof commented May 28, 2018

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

@rayrutjes
Copy link
Member

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.

@yratof
Copy link
Author

yratof commented May 28, 2018

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

@rayrutjes
Copy link
Member

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?

@yratof
Copy link
Author

yratof commented May 29, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants