Change displayed name of facet based on Shopify tags

Another topic discussed how to change the displayed name of a facet based on a Shopify metafield. See here:

I’m interested in how that can be done for facets based on Shopify product tags.

I’m pretty the code involved is this code from algolia_facets.js.liquid:
algolia.facetDisplayFunctions = {
price_range: function displayRange (value) {
var values = value.split(’:’);

return _.map(values, function (e) {
  return algolia.formatMoney(+e * 100).replace(/\.\d+/, '');
}).join(' - ');

},
’meta.ships_in’: function displayName(name) {
if (name == ‘available’) {
return ‘21+ days’;
}
}
}

Specifically, I suspect the bolded part is where a reference to the Shopify tag would go. But I already sense a problem because the tags for a product in Shopify are in a comma delimited list.

if I have a tag in Shopify that’s named “level_Level-1” for example, how can I make the facet list display just “Level-1”?

Thanks.

Hi @grouphiit,

Could you provide an example of your record? That would help to better understand which part you want to rename. I’ll use an example to explain the code above. Let’s say we have a record with the following structure:

{
  "name": "shirt",
  "level0": {
    "level1": {
      "sizes": ["small", "medium", "large"]
    }
  }
}

We want to display S rather than the plain word “small”. You’re right the part in bold is the path to the attribute we want to update. In our example the path is level0.level1.sizes. Even though it’s a list in the records the displayName function will be called three times with each element of the list.

algolia.facetDisplayFunctions = {
  price_range: /* ... */,
  "level0.level1.sizes": function displaySizes(name) {
    if (name === "small") {
      return "S";
    }

    return name;
  },
};

Hope that helps!