How to omit products or pages tagged with 'wholesale' in search results

Solved
Shopify Partner
18 0 0

Ok, so I'm trying to tweak my search template so it doesn't returns results relating to wholesale products or pages.

 

I've been advised to use this code below but it doesn't relate to my search template (I'm using Canopy theme).

 

Here's what I'm trying to implement:

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

And here's the original code from my search template

 
  <!-- Begin results -->
  <div class="row grid" data-normheights=".image" data-normheights-inner="img">
    {% for item in search.results %}
{%- include 'bold-product' with item, hide_action: 'skip' -%}
    {% if item.object_type == 'product' %}
      {% assign product = item %}
      {% include 'product-block' %}
    {% else %}
    
    {% assign mod4 = forloop.index | modulo: 4 %}
    {% assign mod3 = forloop.index | modulo: 3 %}
    {% assign mod2 = forloop.index | modulo: 2 %}
    <div class="product-block page-block flex column max-cols-{{ section.settings.products_per_row }} {% if mod2 == 1 %} nth-2{% endif %}{% if mod3 == 1 %} nth-3{% endif %}{% if mod4 == 1 %} nth-4{% endif %}">
      <div class="text-cont">
        {% if item.object_type == 'article' and item.image %}
        <div class="article-image">{{ item.image | img_url: 'grande' | img_tag | link_to: item.url }}</div>
        {% endif %}
        <h4><a href="{{ item.url }}">{{ item.title }}</a></h4>
        <div><a href="{{ item.url }}">{{ item.content | strip_html | truncatewords: 25 | highlight: search.terms }}</a></div>
        <a class="more-link" href="{{ item.url }}">{{ 'general.search.read_more' | t }}</a>
      </div>
    </div>
    {% endif %}
    {% endfor %}
  </div>
  <!-- End results -->
  

Unfortunately I don't know liquid well enough to know how to implement the first section of code into the second, can anyone help at all?

TIA, Sarah :)

0 Likes
Shopify Partner
18 0 0

Just bumping up to see if anyone can help? :)

0 Likes

Success.

Astronaut
1088 149 274

Just try the code you were given. You can see there's an unless statement started immediately following the forloop, and closes immediately before the forloop ends. Try the same thing:

 

 <div class="row grid" data-normheights=".image" data-normheights-inner="img">
    {% for item in search.results %}
    {% unless item.tags contains 'wholesale' %}
{%- include 'bold-product' with item, hide_action: 'skip' -%}
    {% if item.object_type == 'product' %}
      {% assign product = item %}
      {% include 'product-block' %}
    {% else %}
    
    {% assign mod4 = forloop.index | modulo: 4 %}
    {% assign mod3 = forloop.index | modulo: 3 %}
    {% assign mod2 = forloop.index | modulo: 2 %}
    <div class="product-block page-block flex column max-cols-{{ section.settings.products_per_row }} {% if mod2 == 1 %} nth-2{% endif %}{% if mod3 == 1 %} nth-3{% endif %}{% if mod4 == 1 %} nth-4{% endif %}">
      <div class="text-cont">
        {% if item.object_type == 'article' and item.image %}
        <div class="article-image">{{ item.image | img_url: 'grande' | img_tag | link_to: item.url }}</div>
        {% endif %}
        <h4><a href="{{ item.url }}">{{ item.title }}</a></h4>
        <div><a href="{{ item.url }}">{{ item.content | strip_html | truncatewords: 25 | highlight: search.terms }}</a></div>
        <a class="more-link" href="{{ item.url }}">{{ 'general.search.read_more' | t }}</a>
      </div>
    </div>
    {% endif %}
    {% endunless %}
    {% endfor %}
  </div>

Make sure you're tagging your products 'wholesale" and not "Wholesale" or anything else. I imagine this will work.

 

 

If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes
Shopify Partner
18 0 0

Thanks so much for your help! This is what I'd been trying myself however I was getting an alert saying it couldn't be saved due to an error, so I thought my coding was wrong. It turns out it was a glitch in the backend rather than the code, the code works perfectly now I've found a workaround for saving it :)

Thank you!

0 Likes
New Member
2 0 0

This code works perfectly thank you for sharing it.

I would like to thank you in advance and ask if is possible to have customers tagged with 'wholesale' to see only items tagged with 'wholesale'.

 

Another problem is the number of items shown on the total results which include the hidden items.

Does anyone have a solution for this question?

Below is my current code.

 

 

{% paginate search.results by 20 %}

<div class="page-width">
  <header class="grid medium-up--grid--table section-header small--text-center">
    <div class="grid__item medium-up--two-thirds section-header__item">
      <h1 class="section-header__title">{{ 'general.search.title' | t }}</h1>
        {% if search.performed %}
          <p class="section-header__subtext">{{ 'general.search.results_count_html' | t: count: search.results_count, terms: search.terms }}</p>
        {% endif %}
    </div>
    <div class="grid__item medium-up--one-third section-header__item">
      <form action="/search" method="get" class="input-group" role="search">
        {% comment %}<input type="hidden" name="type" value="product">{% endcomment %}
        <input type="search" name="q" value="{{ search.terms | escape }}" placeholder="{{ 'general.search.placeholder' | t }}" aria-label="{{ 'general.search.placeholder' | t }}" class="input-group__field input--content-color">

        <div class="input-group__btn">
          <button type="submit" class="btn btn--narrow">
            {% include 'icon-arrow-right' %}
            <span class="icon__fallback-text">{{ 'general.search.submit' | t }}</span>
          </button>
        </div>
      </form>
    </div>
  </header>

  {% if search.performed %}
    {% comment %}
      Find the number of products and pages in the search results
    {% endcomment %}
    {% assign product_count = 0 %}
    {% assign page_count = 0 %}
    {% for item in search.results %}

        		{% if item.object_type == 'product' %}
                  {% assign product_count = product_count | plus: 1 %}
                {% else %}
                  {% assign page_count = page_count | plus: 1 %}
                {% endif %}

    {% endfor %}

    {% if product_count > 0 %}
      <h2 class="h4 small--text-center">{{ 'general.search.products' | t }}</h2>

      <div class="grid grid--no-gutters grid--uniform">
        {% for item in search.results %}        		
        		{% unless item.tags contains 'WHOLESALE' %} 
        	     {% if item.object_type == 'product' %}
                    <div class="grid__item small--one-half medium-up--one-fifth">
                      {% include 'product-card', product: item %}
                    </div>
        		 {% endif %}
        		{% endunless %}
        {% endfor %}
      </div>
    {% endif %}

    {% if product_count and page_count %}
      <hr>
    {% endif %}

    {% if page_count > 0 %}
      <h4 class="small--text-center">{{ 'general.search.pages_and_articles' | t }}</h4>

      {% if customer %}
        <div class="content-block content-block--no-bottom-padding">
          <div class="grid grid--uniform">
          {% for item in search.results %}
                  {% unless item.object_type == 'product' %}
                    <div class="grid__item medium-up--one-third content-block__item">
                      <h5>{{ item.title | link_to: item.url }}</h5>
                      <div class="rte">{{ item.content | strip_html | truncate: 150 }}</div>
                      <p><a href="{{ item.url }}"><strong>{{ 'general.search.read_more' | t }}</strong></a></p>
                    </div>
                  {% endunless %}
          {% endfor %}
          </div>
        </div>
      {% endif %}   
    {% endif %}


      {% if paginate.pages > 1 %}
        <div class="pagination">
          {{ paginate | default_pagination | replace: '&laquo; Previous', '&larr;' | replace: 'Next &raquo;', '&rarr;' }}
        </div>
      {% endif %}

  {% endif %}  
</div>

{% endpaginate %}

 

 

0 Likes