Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Hiding products, pages & collections from Shopify search results using tags?

Hiding products, pages & collections from Shopify search results using tags?

Eileeno
New Member
4 0 0

Hello, i've created a separate wholesale section on my website that uses tags to define customers as 'wholesale' i'm now trying to hide my 'wholesale' tagged products from the Shopify search results.

 

When a customer logs in and is tagged with 'wholesale' they have a separate menu, products & pages that are not available to 'non-wholesale' customers.


I've recently updated my theme from 'Debut' to 'Broadcast' and i'm trying to copy over the coding from my search.liquid but i'm having trouble getting it to work as it currently does.

 

Debut Theme - Hiding pages, products & collections tagged - 'Wholesale'

 

 

 

 

 

<!-- /templates/search.liquid -->


{% assign respond_js_secret_key = shop.domain | md5 %}
{% unless search.terms == respond_js_secret_key %}


{% paginate search.results by 10 %}
<div class="page-width">
    {% if search.performed == false %}
    <div class="text-center">
      <h1 class="h2">{{ 'general.search.title' | t }}</h1>
  {% else %}
    <div class="section-header text-center">
      <h1 class="h2">
        <span class="visually-hidden">{{ 'general.search.heading' | t: count: search.results_count }}:</span>
        {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
      </h1>
  {% endif %}
      <div class="grid">
        <div class="grid__item medium-up--two-fifths medium-up--push-three-tenths">
          <form action="/search" method="get" role="search" class="search">
            <div aria-live="polite" class="form-message form-message--error search-result-error-message hide" data-search-error-message>
              <ul id="error-search-form">
                <li>{{ 'general.search.empty_search_message' | t }}</li>
              </ul>
            </div>

            <div class="input-group">
              <input type="search"
                id="SearchInput"
                class="input-group__field search__input"
                name="q"
                value="{{ search.terms | escape }}"
                placeholder="{{ 'general.search.placeholder' | t }}"
                aria-label="{{ 'general.search.placeholder' | t }}"
                >
              <span class="input-group__btn">
                <button id="SearchResultSubmit" class="btn search__submit" type="submit">{{ 'general.search.submit' | t }}</button>
              </span>
            </div>
          </form>
        </div>
      </div>

      {% if search.performed and search.results_count == 0 %}
        <hr class="hr--small hr--invisible" aria-hidden="true" />
        <div class="rte">
          <p>{{ 'general.search.no_results_html' | t: terms: search.terms }}</p>
        </div>
      {% endif %}
  </div>
</div>


 
       <div class="product-card product-card--list">
              <a href="{{ item.url }}" class="full-width-link">
                <span class="visually-hidden">{{ item.title }}</span>
              </a>
              <div class="list-view-item__link">
                {% if item.image %}
                  <div class="list-view-item__image-column">
                    <div class="list-view-item__image-wrapper">
                      <img class="list-view-item__image" src="{{ item.image.src | img_url: '600x600' }}" alt="{{ item.image.alt | escape }}">
                    </div>
                  </div>
                {% endif %}

                <div class="list-view-item__title-column">
                  <div class="list-view-item__title" aria-hidden="true">{{ item.title }}</div>
                  <div>
                    {% if item.published_at %}{{ item.published_at | date: "%b %d, %Y" }} &#8212; {% endif %}
                    {{ item.content | strip_html | truncate: 200 }}
                  </div>
                </div>
              </div>
            </div>
      
      
      {% if search.performed %}
        
         <div class="page-width">
        <div class="grid-uniform">

          {% for item in search.results %}
{% assign itemIswholesale = false %}
{% if item.tags contains 'wholesale' or item.title contains 'wholesale' %}
{% assign itemIswholesale = true %}
{% endif %}

{% if itemIswholesale and customer and customer.tags contains 'wholesale' %}
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-card-list', product: item %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% else %}
{% unless itemIswholesale %} 
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-card-list', product: item %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% endunless %}
{% endif %}
{% endfor %}
        </div>
              </div>
        {% if paginate.pages > 1 %}
          {% include 'pagination' %}
        {% endif %}

      {% endif %}

    </div>
 
{% endpaginate %}

{% else %}
  {% include 'respond' %}
  {% layout none %}
{% endunless %}

 

 

 

 

 

 New Broadcast Theme: 

 

 

 

 

 

{%- liquid
  assign enable_sort = section.settings.enable_sort
  assign enable_filters = section.settings.enable_filters
  assign filter_layout = section.settings.filter_layout

  if filter_layout == 'inline-open'
    assign show_group_filters = true
  endif
-%}

{%- capture collection_sidebar_class -%}
  {%- if filter_layout == 'slide-out' -%}
    collection__sidebar__slide-out
    {%- else -%}
    collection__sidebar__slider{% if show_group_filters %} expanded drawer--animated no-mobile-animation{% endif %}
  {%- endif -%}
{%- endcapture -%}

{%- style -%}
  .search-page {
    --PT: {{ section.settings.padding_top }}px;
    --PB: {{ section.settings.padding_bottom }}px;
  }
{%- endstyle -%}

<section class="page search-page collection section-padding"
  data-section-type="collection"
  data-section-id="{{ section.id }}"
  data-sort="{{ enable_sort }}">
  <main-search>
  <div class="search-outer">
    <form action="{{ routes.search_url }}" class="search-form">
      <input name="options[prefix]" type="hidden" value="last">
      <div class="input-holder">
        <input
          type="search"
          name="q"
          class="search-box"
          aria-label="{{ 'general.search.placeholder' | t }}"
          placeholder="{{ 'general.search.title' | t }}"
          value="{{ search.terms }}">
          <button type="reset" class="search-reset{% if search.terms == blank %} hidden{% endif %}" aria-label="{{ 'general.search.reset' | t }}">
            {{- 'general.search.clear' | t -}}
          </button>
        </div>
      <button type="submit" class="btn btn--primary btn--solid search-submit" aria-label="{{ 'general.search.search' | t }}">{%- render 'icon-search' -%}</button>
    </form>
  </div>
</main-search>

  {%- if search.performed -%}
    {%- if search.results == empty -%}
      <p class="search__caption caps">{{ 'general.search.no_results' | t: terms: search.terms }}</p>
    {%- else -%}
      <p class="search__caption caps">{{ 'general.search.results_with_count_and_term' | t: terms: search.terms, count: search.results_count }}</p>
    {%- endif -%}

    {%- liquid
      assign nav_classes = ''
      if enable_sort
        assign nav_classes = nav_classes | append: ' collection__nav--sort'
      endif

      if enable_filters
        assign nav_classes = nav_classes | append: ' collection__nav--filter'
      endif
    -%}

    {%- if enable_sort or enable_filters -%}
      {%- liquid
        assign filter_active_count = 0

        for filter in search.filters
          assign filter_active_count = filter_active_count | plus: filter.active_values.size
        endfor
      -%}
      <nav class="collection__nav{{ nav_classes }}" data-collection-nav>
        {%- if enable_filters -%}
          <div class="popout--group">
            <button
              type="button"
              class="popout__toggle{% unless filter_layout == 'slide-out' %} popout__toggle--filters{% endunless %}"
              aria-expanded="{% if show_group_filters %}true{% else %}false{% endif %}"
              aria-controls="filter-groups"
              data-aria-toggle>
              {%- render 'icon-filter' -%}

              {%- if filter_layout == 'slide-out' -%}
                {{- 'collections.general.filters' | t -}}
              {%- else -%}
                <span class="popout__toggleable-text">
                  <span class="popout__expanded-show">
                    {{- 'collections.general.show_filters' | t -}}
                    <span class="filter-count{% if filter_active_count < 1 %} hidden{% endif %}" data-active-filters>
                      {{- filter_active_count -}}
                    </span>
                  </span>

                  <span class="popout__expanded-hide">
                    {{- 'collections.general.hide_filters' | t -}}
                    <span class="filter-count{% if filter_active_count < 1 %} hidden{% endif %}" data-active-filters>
                      {{- filter_active_count -}}
                    </span>
                  </span>
                </span>
              {%- endif -%}

              {%- unless filter_layout == 'slide-out' -%}
                {%- render 'icon-nav-arrow-down' -%}
              {%- endunless -%}
            </button>
          </div>
        {%- endif -%}
        {%- if enable_sort -%}
          {% render 'collection-sorting' %}
        {%- endif -%}
      </nav>
    {%- endif -%}

    <div class="collection__products{% if enable_filters %} collection__products--group-tags{% endif %}">
      {%- if enable_filters -%}
        <div class="{{ collection_sidebar_class }}" id="filter-groups" data-collection-sidebar>
          <div class="collection__sidebar__head{% unless filter_layout == 'slide-out' %} mobile{% endunless %}">
            <h3>
              {{- 'collections.general.filters' | t -}}

              <span
                class="filter-count{% if filter_active_count < 1 %} hidden{% endif %}"
                data-active-filters>
                {{- filter_active_count -}}
              </span>
            </h3>

            <a href="#filters-group" class="collection__sidebar__close" data-collection-sidebar-close aria-label="{{ 'collections.general.hide_filters' | t }}">
              {%- render 'icon-cancel' -%}
            </a>
          </div>

          {% render 'collection-filters-sidebar', section: section %}
        </div>
      {%- endif -%}

      <span class="drawer__underlay" data-collection-sidebar-close></span>

      {%- paginate search.results by 36 -%}
        <div class="grid-outer" data-products-grid>
          <div class="grid" id="SearchLoop">
            {%- if search.results == empty -%}
              <div class="no-results">
                <p><strong>{{ 'collections.general.no_matches' | t }}</strong></p>
                {%- liquid
                  assign sort_by_string = ''
                  assign sort_by = search.sort_by
                  assign string_connector = '?'

                  if sort_by != blank
                    assign sort_by_string = '?sort_by=' | append: sort_by
                    assign string_connector = '&'
                  endif
                  assign option_prefix = 'options[prefix]' | url_encode

                  assign search_parameters = string_connector | append: 'type=' | append: terms | append: '&' | append: option_prefix | append: '=last' | append: '&q=' | append: search.terms
                -%}
                <a class="btn btn--primary btn--solid" href="{{ request.path | append: sort_by_string | append: search_parameters }}" data-filter-update-url><span>{{ 'collections.general.reset' | t }}</span></a>
              </div>
            {%- else -%}
              {%- liquid
                for item in search.results
                  if item.object_type == 'product'
                    render 'product-grid-item', product: item, index: forloop.index
                  else
                    render 'search-results-item', item: item, tabindex: true
                  endif
                endfor
              -%}
            {%- endif -%}
          </div>

          {%- render 'pagination', paginate: paginate -%}

          <div class="grid__loader">
            <div class="loader grid__loader-line"><div class="loader-indeterminate"></div></div>
          </div>
        </div>
      {%- endpaginate -%}
    </div>
  {%- endif -%}
</section>

      {% if search.performed %}
        
         <div class="page-width">
        <div class="grid-uniform">

          {% for item in search.results %}
{% assign itemIswholesale = false %}
{% if item.tags contains 'wholesale' or item.title contains 'wholesale' %}
{% assign itemIswholesale = true %}
{% endif %}

{% if itemIswholesale and customer and customer.tags contains 'wholesale' %}
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-card-list', product: item %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% else %}
{% unless itemIswholesale %} 
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-card-list', product: item %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% endunless %}
{% endif %}
{% endfor %}
        </div>
              </div>
        {% if paginate.pages > 1 %}
          {% include 'pagination' %}
        {% endif %}

      {% endif %}

    </div>

  {% include 'respond' %}
  {% layout none %}

{% schema %}
  {
    "name": "Search",
    "templates": ["search"],
    "settings": [
      {
        "type": "checkbox",
        "id": "enable_sort",
        "label": "Show sorting",
        "default": true
      },
      {
        "type": "checkbox",
        "id": "enable_filters",
        "label": "Enable filters",
        "default": true
      },
      {
        "type": "select",
        "id": "filter_layout",
        "label": "Filter layout",
        "default": "inline-open",
        "options": [
          { "label": "Slide out", "value": "slide-out" },
          { "label": "Inline (closed)", "value": "inline-closed" },
          { "label": "Inline (open)", "value": "inline-open" }
        ]
      },
      {
        "type": "link_list",
        "id": "collection_linklist",
        "label": "Sidebar navigation",
        "info": "This menu won't show dropdown items."
      },
      {
        "type": "header",
        "content": "Section padding"
      },
      {
        "type": "range",
        "id": "padding_top",
        "min": 0,
        "max": 100,
        "step": 1,
        "unit": "px",
        "label": "Padding top",
        "default": 50
      },
      {
        "type": "range",
        "id": "padding_bottom",
        "min": 0,
        "max": 100,
        "step": 1,
        "unit": "px",
        "label": "Padding bottom",
        "default": 50
      }
    ]
  }
{% endschema %}

 

 

The search results are still showing all products, I think its because something is misplaced in here?

 

Thank you in advance for your help, Nick & Eileen

Reply 1 (1)

MartinaIct
Shopify Partner
5 0 0

Hi, to understand better, you have:

  • wholesale tagged products
  • wholesale tagged customers
  1. If I'm not logged in I have to see all products?
  2. If I'm logged in but not tagged with 'wholesale' I have to see all products, or only non-tagged products?
  3. If I'm logged in and tagged, I have to see all products or only wholesale products?