Hide Products from Search Results based on Product Tag

Highlighted
Excursionist
24 0 10

I got it to work by making a new type of product called “xsearch". Then I added a new IF paramenter around the whole existing search IF parameter — so mine is now the first IF and the existing one you need to find is the second one shown here: 

      				{% if item.type != 'xsearch' %}
      					{% if item_type contains 'Product' %}

and of course closed it after all the pre-existing parameters & code: 

						{% endif %}
					{% endif %}

The items I am hiding are only for a secret special offer. So I can afford to put them in a specialized product type container called “xsearch”. If you need your items to still be in another type of container like “tshirts”, etc this will not work for you. 

If I could find a way to detect the collection of the product result, then this would be fixed easier by detecting for something like “product.collection.handle” — but that returns nothing. I cannot find how to format for finding the resulting item’s collection name or handle or anything. 

0 Likes
Highlighted
Shopify Expert
9812 92 1576

Backtracking to this code:

{% if item_type contains 'Product' and item.product.tag != 'xsearch' %}

^ That isn't valid. You should not need contains to test for the product. Better to use == as it will be a perfect match, so:

{% if item.object_type == 'product' ...

A product has tags, but not tag. See the Liquid reference for product. product.tags will be an array, so with that in mind:

{% if item.object_type == 'product' and item.tags contains 'some-tag' %}
  ..
{% endif %}

If you need to test if the tag does NOT exist:

{% assign showProduct = true %}
{% if item.tags contains 'some-tag' %}{% assign showProduct = false %}{% endif %}
{% if item.object_type == 'product' and showProduct %}
  ..
{% endif %}

or

{% if item.object_type == 'product' %} 
  {% assign showProduct = true %}
  {% if item.tags contains 'some-tag' %}{% assign showProduct = false %}{% endif %}
  {% if showProduct %}
    ..
  {% endif %}
{% endif %}

 

FYI: A product also has a collections object.
With that you can detect what collection the product is within.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Highlighted
Excursionist
24 0 10

Cool — I prefer to just keep the product type in my case, but someone else may find the tag to hide useful. 

0 Likes
Highlighted
New Member
5 0 0

Hi Jason & Thomas. Thanks for your helping out here. Trying to get a tag filtered out for a client. I set up a tag called Wholesale. I tried putting this on search.liquid but maybe I am not putting it in the right place.  Is the text in bold required? I just used Jason's code as it was more complete but not sure if that was supposed to wrap around some other text and the .. was just there for example.

 

% if item.object_type == 'product' %} 
  {% assign showProduct = true %}
  {% if item.tags contains 'Wholesale' %}{% assign showProduct = false %}{% endif %}
  {% if showProduct %}
    ..
  {% endif %}
{% endif %}

 

Also, I put it below in the area below in the bold section on search.liquid.

 

 

{% if search.results.size > 0 %}
        <div class="search-results rows-of-3">
          {% for item in search.results %}
          
        I PUT THE CODE HERE

            {% if item.object_type == 'product' %}
              {% include "product-list-item" with item %}
            {% else %}
              <div class="search-results-item clearfix">

                <div class="search-results-data">
                  {% if item.object_type == 'article' %}
                    {% if item.image %}
                      <img class="item-image" src="{{ item.image | img_url: 'grande' }}" alt="{{ item.title }}">
                    {% endif %}
                    <h3><a href="{{ item.url }}">{{ item.title }}</a></h3>
                    <p class="meta">{{ item.published_at | date: "%b %d, %Y" }}</p>
                    <p>{{ item.excerpt_or_content | strip_html | truncatewords: 30 | highlight: search.terms }}</p>
                  {% else %}
                    <h3><a href="{{ item.url }}">{{ item.title }}</a></h3>
                    <p>{{ item.content | strip_html | truncatewords: 30 | highlight: search.terms }}</p>
                  {% endif %}

                  {% assign readMoreText = 'Read more' %}
                  {% if settings.blog-read-more-text != blank %}
                    {% assign readMoreText = settings.blog-read-more-text %}
                  {% endif %}

                  <p><a class="post-read-more meta" href="{{ item.url }}">{{ readMoreText }}</a></p>
                </div>

              </div> 

 

I ALSO TRIED It here  {% endif %}

 

Trying to hide a few products from the general search as I don't want the public to see the wholesale pricing.

 

Thanks!

0 Likes
Highlighted
Explorer
57 0 30

I have been following this thread, as I too would like to have specific products hidden from the search results - specifically the free digital downloads I have (that are tagged with "download").

Like @LunaMedia, I tried to put in the code that @Jason mentioned above, but wasn't sure exactly where to put it. I tried it in several places within the search.liquid, but am obviously clueless.

Originally, I changed the search.liquid area from...

{% for item in search.results %}
{% include 'search-result' %}
<hr>
{% endfor %}

To... 

{% for item in search.results %}
{% unless item.product.tags contains 'download' %}
{% include 'search-result' %}
<hr>
{% endunless %}
{% endfor %}

But that didn't work. So then I tried...

{% for item in search.results %}
{% unless item.tags contains 'download' %}
{% include 'search-result' %}
<hr>
{% endunless %}
{% endfor %}

...removing the word product from the second line. That didn't work either.

I am not a programmer - just barely understand liquid (though I am trying to learn) - but it SEEMS like the code above should work.

I have been searching for this solution for months, have read a lot of articles, but nothing I've done works to hide these free "products" from search. I'm sure it's something I am not doing correctly.

I read through all the ideas in this thread - did anyone ultimately get this to work? If so - how did you do it?  Thanks in advance for sharing!

0 Likes
Highlighted
New Member
4 0 0

Hi Wesley, 

I tried what you suggessted, but I'm confused- it won't let me save the code because there is an error with the closing tags. Can you help?

0 Likes
Highlighted
New Member
3 0 0

Hello there,

I apologise for bringing up this annoying subject, but i just cant get a tagged product to stay out of the search... Some details:

1. I wont paste all the code in, but in my search.liquid I have:

   {% for item in search.results %}
      {% unless item.tags contains 'wholesale' %}

...... search result code deleted here .....

    {% endif %}
  {% endif %}

So as far as i can see I am filtering the tag 'wholesale' correctly.

2. My test product - you can see here the tag is applied to the saved product https://www.evernote.com/l/AAIFUXXcU_tMd79TSPEHSIN8LD-u1JEwv-4

But... you can see on my search result page the product still appears https://www.evernote.com/l/AAKCkFr6j8JLNrg-yVfmxiyPXcE5ekOa7Qo

If anyone has some debug techniques they could suggest so I could pinpoint where the issue occurs, that would be greatly appreciated.

Kind regards, Steve.

0 Likes
Highlighted
Shopify Expert
9812 92 1576

This code would successfully hide items from the search results based on a tag.

{% for item in search.results %}
  {% unless item.tags contains 'foo' %}
    Showing a product without the foo tag
  {% endunless %}
{% endfor %}

If you've followed this and the product still show I'd have to assume that either:

  • Your code is wrong
  • The code has not been added to the correct file / place
  • You've not changed all instances of the code

I would suggest that you also add something to help you debug this easier. Assuming this is not a published theme add this into your item loop:

<p>Tags: {{ item.tags | json }}</p>

If no tag shows you know right away you're not editing the right section of code. If the "wholesale" tag doesn't show in the list, you know it's related to errors in your tagging (so double check your tags).

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Highlighted
New Member
3 0 0

Hi Jason,

Many thanks for your reply, greatly appreciated.

Your logic all makes sense, and I realise I must be missing something really obvious... It seems I am editing the right page for search, but the loop that is inside this paged is not the loop I am looking for... 

Just to show what I mean, the search code is referenced by search.liquid to live in search-template.liquid - so editing that file I have this:

{% assign search_pagination = section.settings.pagination_limit %}
{% paginate search.results by search_pagination %}

<div class="sixteen columns clearfix collection_nav">
  <h1 class="collection_title {% if search.performed %}collection_title_tags{% endif %}">HELLO{{ 'general.search.title' | t }}</h1>
  {% if search.performed %}
    <ul class="collection_menu">
      <li>
        HELLO2{{ 'general.search.results_count' | t: count: search.results_count }}
      </li>
    </ul>
  {% endif %}
</div>

{% comment %}Check to see if sidebar should be enabled{% endcomment %}
{% if section.blocks.size > 0 %}
  {% assign sidebar = true %}
{% else %}
  {% assign sidebar = false %}
{% endif %}

{% if search.performed %}

  {% if search.results == empty %}
    <br class="clear" />
    <br class="clear" />
    <div class="clearfix">
      <p class="quote">{{ 'general.search.no_results_html' | t: terms: search.terms }}</p>
    </div>

    <br class="clear" />
    <br class="clear" />
  {% else %}

 {% if settings.search_option == 'everything' or search.results.first.price == blank %}

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

    {% for item in search.results %}
      {% unless item.tags contains 'wholesale' %}
      <div class="product_row">
        <p>Tags: {{ item.tags | json }}</p>
        {% assign featured_image = false %}

        {% if item.object_type == 'article' and item.image %}
          {% assign featured_image = true %}
          <div class="four columns alpha">
             <a href="{{ item.url }}" title="{{ item.title | escape }}">
                <img src="{{ item.image | img_url: 'grande' }}"  alt="{{ item.title | escape }}" />
             </a>
          </div>
        {% elsif item.featured_image %}
          {% assign featured_image = true %}
          <div class="four columns alpha">
             <a href="{{ item.url }}" title="{{ item.title | escape }}">
                <img src="{{ item.featured_image | product_img_url: 'grande' }}"  alt="{{ item.title | escape }}" />
             </a>
          </div>
        {% endif %}

        {% if sidebar %}
          <div class="{% if featured_image %}eight{% else %}twelve{% endif %} columns omega align_left">
        {% else %}
          <div class="{% if featured_image %}twelve{% else %}sixteen alpha{% endif %} columns omega align_left">
        {% endif %}

          <h5 class="sub_title">
            <a href="{{ item.url }}" title="{{ item.title | escape }}">LETS PRINT PRICE INFO 4{{ item.title }}</a>
          </h5>

          {% if item.price %}
             <div class="info">
                <span class="price">
                  {% if item.compare_at_price_max > item.price %}
                    <span class="was_price">
                      <span class="money">{{ item.compare_at_price_max | money }}</span>
                    </span>
                  {% endif %}

                  {% if item.available %}
                    {% if item.price_varies %}
                      {% if item.compare_at_price_max > item.price %}
                        <br />
                      {% endif %}
                      <small><em>{{ 'products.general.from' | t }}</em></small>
                    {% endif %}
                    <span class="money">{{ item.price_min | money }}</span>
                  {% else %}
                    <span class="money">{{ item.price_min | money }}</span> - {{ 'products.product.sold_out' | t }}
                  {% endif %}
                LETS PRINT PRICE INFO666</span>
            </div>
          {% endif %}

          {% if item.object_type == 'article' %}
            <p class="blog_meta">
              {% if section.settings.blog_author %}
                <span>{{ 'blogs.article.by_author' | t: author: item.author }}</span>
              {% endif %}

              {% if section.settings.blog_date %}
                <span>{{ item.published_at | date: format: "month_day_year" }}</span>
              {% endif %}
            </p>
          {% endif %}

          {% if item.excerpt %}
            <div class="excerpt">{{ item.excerpt }}</div>

            {% if section.settings.read_more_link %}
            <p>
              <a href="{{ item.url }}" title="{{ item.title | escape }}">{{ 'blogs.general.continue_reading_html' | t }}</a>
            </p>
          {% endif %}
          {% else %}
            <p>
              {{ item.content | strip_html | truncatewords: 40 | highlight: search.terms | replace: 'Description', '' | replace: 'Specs', '' | replace: 'Shipping', '' | replace: 'Size', '' }}
            </p>
          {% endif %}
        </div>
      </div>
    <hr />
        {% endunless %}
   {% endfor %}
  </div>

 {% else %}
      {% 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 %}
      {% include 'product-loop', sidebar: sidebar %}

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

  {% include 'pagination' %}

{% else %}
  <div class="clearfix search_page">
    <br class="clear" />
    <br class="clear" />
    <div class="ten columns offset-by-three columns center">
      <p class="quote">
        {{ 'general.search.description' | t }}HELLO3333
      </p>
    </div>
      <form class="search" action="/search">
        <div class="four offset-by-five columns center">
          {% if settings.search_option != 'everything' %}
            <input type="hidden" name="type" value="product" />
          {% endif %}
          <input type="text" name="q" placeholder="{{ 'general.search.placeholder' | t }}" value="{{ search.terms }}" x-webkit-speech autocapitalize="off" autocomplete="off" autocorrect="off" />

      </div>
      <div class="two columns">
        <input type="submit" name="submit" class="action_button" value="{{ 'general.search.submit' | t }}" style="margin-top:0" />
      </div>
      </form>
    </div>
  </div>
{% endif %}
{% endpaginate %}

{% schema %}

{
  "name": "Search",
  "class": "search-template-section",
  "settings": [
    {
      "type": "select",
      "id": "products_per_row",
      "label": "Products per row",
      "options": [
        {
          "value": "2",
          "label": "2"
        },
        {
          "value": "3",
          "label": "3"
        },
        {
          "value": "4",
          "label": "4"
        }
      ]
    },
    {
      "type": "select",
      "id": "pagination_limit",
      "label": "Products per page",
      "options": [
        {
          "value": "24",
          "label": "24"
        },
        {
          "value": "36",
          "label": "36"
        },
        {
          "value": "48",
          "label": "48"
        }
      ]
    },
    {
      "type": "header",
      "content": "Blog results"
    },
    {
      "type": "checkbox",
      "id": "blog_author",
      "label": "Show author"
    },
    {
      "type": "checkbox",
      "id": "read_more_link",
      "label": "Show continue reading link for blog post excerpts",
      "default": true
    },
    {
      "type": "checkbox",
      "id": "blog_date",
      "label": "Show date",
      "default": true
    },
    {
      "type": "header",
      "content": "Sidebar"
    },
    {
      "type": "paragraph",
      "content": "Create a sidebar by adding content blocks."
    },
    {
      "type": "checkbox",
      "id": "toggle",
      "label": "Toggle sidebar content"
    }
  ],
  "blocks": [
    {
      "type": "filter_by_collection",
      "name": "Collection list",
      "limit": 1
    },
    {
      "type": "filter_by_type",
      "name": "Type list",
      "limit": 1
    },
    {
      "type": "filter_by_vendor",
      "name": "Vendor list",
      "limit": 1
    },
    {
      "type": "menu",
      "name": "Menu",
      "settings": [
        {
          "type": "link_list",
          "id": "custom_menu_linklist",
          "label": "Menu"
        }
      ]
    },
    {
      "type": "text",
      "name": "Text",
      "settings": [
        {
          "type": "text",
          "id": "title",
          "label": "Heading",
          "default": "Heading"
        },
        {
          "type": "richtext",
          "id": "text",
          "label": "Text",
          "default": "<p>Text area can be used for special announcements or general information.</p>"
        }
      ]
    },
    {
      "type": "page",
      "name": "Page",
      "settings": [
        {
          "type": "page",
          "id": "content_page",
          "label": "Page"
        }
      ]
    }
  ]
}

{% endschema %}

 

Up at the top of the file I have put in a couple of markers, for example:

<div class="sixteen columns clearfix collection_nav">
  <h1 class="collection_title {% if search.performed %}collection_title_tags{% endif %}">HELLO{{ 'general.search.title' | t }}</h1>
  {% if search.performed %}

 

And you can see I've put in your tag display code as well as the tag exclude code further down:

{% for item in search.results %}
      {% unless item.tags contains 'wholesale' %}
      <div class="product_row">
        <p>Tags: {{ item.tags | json }}</p>
        {% assign featured_image = false %}

 

When I then run a search, you can see my markers on the page in the title area, so I am on the right page for this part, BUT none of my markers that are inside the results loop appear including the tag display code... https://www.evernote.com/l/AAL6A49NUt5JBqRNpSZMCwpjr7CoPmK_chw

So it is as if the page loads, but then uses a different search results loop somewhere...

I am using Retina theme - I'll ask their support to take a look at this, but any ideas appreciated. I am guessing this code must use another search result display loop...

Kind regards, Steve.

 

 

 

0 Likes
Highlighted
Shopify Expert
9812 92 1576

Looks like you've got another loop further down your code:

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

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes