Remove words from product categories (facet values) on instant search page

Remove words from product categories (facet values) on instant search page ?

Consider the following product category structure:

Level_1_Category_Name_One
Word_to_delete1 Word_to_delete2 Level_2_Subcategory1 Word_to_delete3
Word_to_delete1 Word_to_delete2 Level_2_Subcategory2 Word_to_delete3
Level_1_Category_Name_Two
Word_to_delete1 Word_to_delete2 Level_2_Subcategory3 Word_to_delete3
Word_to_delete1 Word_to_delete2 Level_2_Subcategory4 Word_to_delete3

Goal: remove “Word_to_delete1 Word_to_delete2 " and remove
" Word_to_delete3” (including the space character) from each of the subcategories.

Understand that I need to use a special property of the hierarchical menu widget to customize the facet values before display.

Any guidance greatly appreciated.

instantsearch.widgets.hierarchicalMenu(options);

options.transformData.item (function)
Method to change the object passed to the item template

On WordPress using:
Algolia Search for WooCommerce v0.9.0
Search by Algolia – Instant & Relevant results v2.10.2
WooCommerce v3.3.3

Hi @ted,

I looked at your issue, I don’t think you can do it easily from the front end.

Indeed you can use the transformData function to update the label attribute on each item. But it’s better if the content of your have always the same structure, otherwise it will be very hard to implement. I mean by that, when the label doesn’t follow the same structure how do we know which part we want to keep / remove? Knowing this, here is an example that show how to use the transformData attribute to update the label of a HierarchicalMenu.

Another solution is to push only the content that you want to display in the Algolia index. It will avoid you to implement the transformation in the front.

Hope that helps, let me know if you have questions.

Hi Samuel,

Thank you and I agree with your concerns. I thought about them beforehand too.

The good news is that the category naming conventions and patterns are highly structured and repetitive, so I shouldn’t need a lot of code. Something as simple as determining the overall length of the subcategory string and removing the first X number of characters and last Y number of characters should do the trick.

So in this case, the code should also isolate only the second level of the hierarchy, not all levels, which my guess is hierarchicalCategories.lvl1.

I tried to convey my pattern by using the same words for all subcategories. Note that Level 0 categories remain unchanged. Only level 1 category names change.

BEFORE:

Level_0_Category_Name_One
Word_to_delete1 Word_to_delete2 Level_1_Subcategory1 Word_to_delete3
Word_to_delete1 Word_to_delete2 Level_1_Subcategory2 Word_to_delete3
Level_0_Category_Name_Two
Word_to_delete1 Word_to_delete2 Level_1_Subcategory3 Word_to_delete3
Word_to_delete1 Word_to_delete2 Level_1_Subcategory4 Word_to_delete3

AFTER:

Level_0_Category_Name_One
Level_1_Subcategory1
Level_1_Subcategory2
Level_0_Category_Name_Two
Level_1_Subcategory3
Level_1_Subcategory4

If you can update your example to reflect this intent, it would be great!

Thank you.