algolia / algoliasearch-wordpress

❌🗑🙅‍♂️ Algolia Search plugin for WordPress is no longer supported. Please use our API client guide instead
https://www.algolia.com/doc/integration/wordpress/getting-started/quick-start/
GNU General Public License v2.0
359 stars 115 forks source link

Displaying hierarchical categories in autocomplete search #759

Open yratof opened 6 years ago

yratof commented 6 years ago

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:

What happened instead?

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

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

Propell1 commented 6 years ago

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 commented 6 years ago

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

yratof commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

rayrutjes commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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