How to add an "or" to shopify instantsearch

I want to filter search results with OR: e.g vendor=“Anvil” OR vendor=“Result”.

I added this to shopify “layout/theme/liquid”:

  {% if collection.title %}
    {% case collection.title %}
      {% when "Caprina"%}
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: "Caprina%20-%20Promotional%20I.T%20Solutions" %}
      {% when "Dex Collection"%}
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: "Dex%20Group%20-%20Dex%20Collection" %}
      {% when "Epicentre - Legend Life"%}
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: "Epicentre%20Trading%20P%2FL%20-%20Legend%20Life" %}
      {% when "Stencil" %}
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: "STENCIL%20-%20Printec%20International%20Trading%20P%2FL" %}
      {% when "Superior Activewear - Anvil, Gildan, Result" %}
       
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: "Anvil" | append: "&" | append: "fR%5Bvendor%5D%5B1%5D=Result" %}
      {% else %}
        {% assign search_filter = "fR%5Bvendor%5D%5B0%5D=" | append: collection.title | replace: " - ", "%2D" | replace: "-", "%20" | replace: "%2D", " - " %}
    {% endcase %}
  {% endif %}
  {% if search_filter %}
<meta content="0; url=/search?{{ search_filter }}" http-equiv="refresh" />  
  {% else %}  

It works fine for a single vendor - but not for merging 2 vendors.

Also, it is a low quality solution, have you suggestions for a better way to filter by collection.

Could you share with us a link to your theme? :slight_smile:
If it’s not the live one, here’s how to get a preview link : https://community.algolia.com/shopify/frequently-asked-questions.html#get-a-preview-link-to-a-theme .
This would be really helpful in order to try out URL parameters.

Removed login information.

You should change your facet type to disjunctive, as described in this screenshot:

By doing so, you are basically activating the filter in OR mode, which changes the applied setting in the front-end.
Your next step would then be to go on the search page and select those filters to see the new parameters reflected in the URL (notice how it says dFR (for Disjunctive Facet Refinements) instead of fR (Facet Refinements).


You asked for suggestions, I believe your solution is definitely good. However, the code per collection could definitely be cleaner.
Instead of the full search filter, you could put inside your switch cases a simple string, with a separator between brands.
For the separator, you’ll need to use a character that you know will never be used in your collection names, in my case I chose |.

  {% assign vendors = "Anvil|Result" %}

Then to transform this into the correct URL parameters, here’s a simple liquid code which does just this:

  {% if vendors %}
    {% assign vendors_arr = vendors | split: '|' %}
    {% assign search_filter = '?' %}
    {% for vendor in vendors_arr %}
      {% if forloop.first == false %}
        {% assign search_filter = search_filter | append: '&' %}
      {% endif %}
      {% assign escaped_vendor = vendor | url_param_escape %}
      {% assign search_filter = search_filter | append: 'dFR%5Bvendor%5D%5B' | append: forloop.index0 | append: '%5D=' | append: escaped_vendor %}
    {% endfor %}
  {% endif %}

As you can see the code is calling url_param_escape, so you should simply write the collection as it is named, not escaped, e.g.
Anvil|Epicentre Trading P/L - Legend Life and not
Anvil|Epicentre%20Trading%20P%2FL%20-%20Legend%20Life.

1 Like