Shopify: How to filter $0 items from auto-complete and search results?

We do not want to show any items that have a price of $0 (e.g., free sample). How do we prevent these items from appearing in the auto-complete results and on the search results page?

To do this with the autocomplete, in the algolia_autocomplete.js.liquid file, you can simply add a filters parameter:

    // ...
    return {
      name: section,
      source: $.fn.autocomplete.sources.hits(params.index, {
        filters: 'price!=0', // <- This line
        hitsPerPage: params.hitsPerPage,
        highlightPreTag: '<span class="aa-highlight">',
        highlightPostTag: '</span>'
      }),
      displayKey: 'title',
      templates: templates
    }

You can use the same logic as described in this other answer to add a filter to the instant search page.
Something like this should work:

instant.search.addWidget({
  // getConfiguration: only needed if you're not already displaying tags in the front-end
  getConfiguration: function () {
    return {
      facets: ['price'] // In case you're not using a price facet in the sidebar
    };
  },
  init: function (opts) {
    var helper = opts.helper;
    var page = helper.getPage();
    helper.addNumericRefinement('price', '!=', 0);
    helper.setPage(page);
  }
})
1 Like

As a follow-up to this question, I’d now recommend you to look at this post which aggregates information from here and other topics: