Hide Products from Search Results based on Product Tag

Highlighted
New Member
3 0 0

Hey Jason,

Many thanks for your reply - and its pointed me to fixing it :)

Just to share the fix - I created a new snippet and copied the code from product-loop snippet to my new search-loop snippet, and then added this switch code in my search-liquid template:

     {% if sidebar %}
        {% include 'sidebar' %}
        <div class="twelve columns">
      {% else %}
        <div class="sixteen columns">
      {% endif %}

      {% assign products = search.results %}
      {% assign products_per_row = section.settings.products_per_row %}
          
          {% if template == 'search' %}
		  {% include 'search-loop', sidebar: sidebar %}
  		  {% else %}
          {% include 'product-loop', sidebar: sidebar %}
          {% endif %}

      </div>
    {% endif %}
  {% endif %}

 

Then I put the tag exclude code into search-loop like this:

{% for product in products limit: limit %}
  {% unless product.tags contains 'wholesale' %}
    {% if product.id == skip_product.id or skip == true %}
      {% assign product_found = true %}
    {% else %}
      {% if forloop.rindex0 == 0 and product_found == false and forloop.length != products.count and template != 'search' %}
        {% assign skip = true %}
      {% else %}
        {% include 'product-thumbnail', sidebar: sidebar %}

        {% if products_per_row == "2" %}
          {% cycle collection_group: '', new_row %}
        {% elsif products_per_row == "3" %}
          {% cycle collection_group: '', '', new_row %}
        {% elsif products_per_row == "4" %}
          {% cycle collection_group: '', '', '', new_row %}
        {% endif %}
      {% endif %}
    {% endif %}
  {% endunless %}
  {% endfor %}

 

Its not the more programatically cool way to do it but it works.

I know have 2 more tweaks to get to the point of fully making this work, any help very appreciated.

1. I see advice above to just remove the Search Result Count number, but to me this sees like reducing the UX badly... In search-template I see the code to display the result count number:

{{ 'general.search.results_count' | t: count: search.results_count }}

so my question is how to redefine the var "search.results_count" before it displays to exclude the tagged items?

2. I see people saying that the pagination will not work correctly - so again, somewhere the pagination count for the records it belies will be displayed is created, and I'd like to subtract the number of results fed into the pagination function so the search results all work as they should.

Any help massively appreciated!

Kind regards, Steve.

0 Likes
Highlighted
Shopify Partner
29 0 1

I'm having the same problem hiding products from users not logged in... would appreciate any help!

	{% for item in search.results %}
	{% unless customer.tags contains 'wholesale' and item.tags contains 'wholesale' %}				
	{% endunless %}
	{% endfor %}

I put this in search-template.liquid - did not work :( - also tried putting in search-result-grid.liquid and search-result.liquid as well.

0 Likes
Highlighted
Shopify Partner
13 0 1

Restrict Searching specific fields - I have a store which keeps pulling in products with wording that is within the descriptions. Which i do not want to show.

Search 'Meat' or 'oil' then not only do tags that have meat but also maybe rice that has meat or items with oil in the ingredients in the description will show.

I know shopify has these product fields which are searched. https://help.shopify.com/ma...
title
handle
body
vendor
product_type
tag
variant
sku

I would like to restrict the search so it doesn't show results from the 'body' of the product?

 

I have tried this solution. So that it doesn't pull in items which have the same word in the description. Which works but wanted to see if anyone can see any flaws in this before setting it live.

{% assign showProduct = false %}
{% assign term_downcase = sanitized_terms | downcase %}
{% assign tag_downcase = item.tags | downcase %}
{% assign title_downcase = item.title | downcase %}
{% assign type_downcase = item.type | downcase %}
{% assign handle_downcase = item.handle | downcase %}
{% assign vendor_downcase = item.vendor | downcase %}
{% assign tag_downcase_s = item.tags | downcase | append: 's' %}
{% assign title_downcase_s = title.tags | downcase | append: 's' %}

{% if tag_downcase contains term_downcase or tag_downcase_s contains term_downcase or title_downcase contains term_downcase or title_downcase_s contains term_downcase or type_downcase contains term_downcase or handle_downcase contains term_downcase or vendor_downcase contains term_downcase %}{% assign showProduct = true %}{% endif %}

{% if showProduct %}
{% if item.object_type != 'product' %}{% continue %}{% endif %}
  ..
{% endif %}

 

Was thinking this could work

{% unless item.description contains 'meat' %}
  ...
{% endunless %}

but it didn't pull in items that were tagged with meat but had it in the description. 

0 Likes
Highlighted
New Member
9 0 0

I used this code on a store.

 <div id="sandBox" class="row product_list grid">
              {%- for item in search.results -%}
              {% unless item.tags contains 'xsearch' %}
              {%- if item.object_type == 'product' -%}
              <div class="product-item {{_class}}" >
                {%- assign product = item -%}
                {%- include 'product-item' -%}
              </div>
              {%- elsif item.object_type == 'article' -%}
              <div class="article-item {{_class}}">
                {%- assign article = item -%}
                {%- include 'article-item' -%}
              </div>
              {%- else -%}
              <div class="other-item {{_class}}">
                {%- include 'other-item' -%}
              </div>
              {%- endif -%}
              {% endunless %}
              {%- endfor -%}
            </div>

And its working fine.

I used unless , which means when the product with the "xsearch" tag with come the search will not perform for that product.
 

aadrashmehta
0 Likes
Highlighted
New Member
9 0 0

Hi,

 <div id="sandBox" class="row product_list grid">
              {%- for item in search.results -%}
              {% unless item.tags contains 'xsearch' %}
              {%- if item.object_type == 'product' -%}
              <div class="product-item {{_class}}" >
                {%- assign product = item -%}
                {%- include 'product-item' -%}
              </div>
              {%- elsif item.object_type == 'article' -%}
              <div class="article-item {{_class}}">
                {%- assign article = item -%}
                {%- include 'article-item' -%}
              </div>
              {%- else -%}
              <div class="other-item {{_class}}">
                {%- include 'other-item' -%}
              </div>
              {%- endif -%}
              {% endunless %}
              {%- endfor -%}
            </div>

just put the unless tag as i have inserted in the code above mentioned. with the product tag which you dont want to be searched.

Regards,
Aadrash Mehta

aadrashmehta
0 Likes
Highlighted
Excursionist
24 0 10

Does this still work? I did this a while back and the items show up again. I am guessing some updates knocked out the code?

0 Likes