How can I hide products from search results when quantity drops to one?

Hi,

We have a product rule when less than one the product does not appear anymore. However the product will still show in the search results. Is there a way to hide this to users cannot see it in the search after quantity goes to one?

Hi @Calum375 ,

What theme are you using? Please send me the theme name, I will check it for you

Hi, its warehouse theme. Thanks

Hi @Calum375 ,

Please send me the code of main-search.liquid file, I will check it.

search.template.liquid

{%- comment -%}We start by creating a lot of useful variables !{%- endcomment -%}

{%- assign view_mode = cart.attributes.collection_layout | default: section.settings.default_view_layout -%}
{%- assign filtered_terms = search.terms -%}

{%- assign items_per_page = cart.attributes.collection_products_per_page | default: 24 -%}

{%- if filtered_terms contains ’ AND ’ -%}
{%- assign filtered_terms = filtered_terms | split: ’ AND ’ | last -%}
{%- else -%}
{%- assign filtered_terms = filtered_terms | split: ‘product_type:’ | last -%}
{%- endif -%}

{%- assign quick_links_menu = linklists[section.settings.quick_links] -%}

{%- if quick_links_menu.links.size > 0 -%}
{%- assign has_filters = true -%}
{%- capture grid_classes -%}1/3–tablet-and-up 1/{{ section.settings.products_per_row }}–desk{%- endcapture -%}
{%- else -%}
{%- assign has_filters = false -%}
{%- capture grid_classes -%}1/3–tablet 1/{{ section.settings.products_per_row }}–lap-and-up{%- endcapture -%}
{%- endif -%}

{%- capture section_settings -%}
{
“currentSortBy”: “”,
“currentTags”: ,
“filterMode”: “”,
“defaultLayout”: {{ section.settings.default_view_layout | json }},
“defaultProductsPerPage”: 24,
“isAutomatic”: true,
“gridClasses”: {{ grid_classes | json }}
}
{%- endcapture -%}

{%- if search.performed and search.results_count > 0 -%}
  1. {{ 'general.breadcrumb.home' | t }} {%- render 'icon', icon: 'arrow-right' -%}
  2. {{ 'search.general.title_with_terms' | t: terms: filtered_terms }}
{%- endif -%}

{%- if search.performed -%}
{%- if search.results_count == 0 -%}

{{ 'search.general.title' | t }}

{{ 'search.general.no_results_with_terms' | t: terms: filtered_terms }}

{{ 'search.general.input_placeholder' | t }}

{% render ‘icon’, icon: ‘search’ %}

{{ ‘search.general.go_back_home’ | t }}

{%- else -%} {%- paginate search.results by items_per_page -%}
{%- if has_filters -%}
{%- if quick_links_menu.links.size > 0 -%}

{{ quick_links_menu.title }}

{%- endif -%}
{%- endif -%}
{%- if search.results_count > 0 -%}
{%- comment -%} IMPLEMENTATION NOTE: unfortunately I was forced to add a lot of extra div (collection__meta, collection__meta-inner, collection__header-inner,...) to be able to accommodate with the brand related layout which make things more complicated... {%- endcomment -%}

{{ 'search.general.products_with_terms' | t: terms: filtered_terms }}

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

{%- assign offset = paginate.current_offset | plus: 1 -%} {%- assign page_size = paginate.current_offset | plus: paginate.page_size | at_most: paginate.items -%}

{{ ‘search.general.showing_count’ | t: offset: offset, page_size: page_size, count: paginate.items }}

{{ ‘collection.general.display’ | t }}:

{{ 'collection.general.page_size' | t: page_size: paginate.page_size }}{%- render 'icon', icon: 'arrow-bottom' -%}
{% render 'icon', icon: 'nav-triangle-borderless' %}
{{ 'collection.general.display' | t }} {% render 'icon', icon: 'close' %}
{{ 'collection.general.page_size' | t: page_size: 24 }} {% render 'icon', icon: 'check-2' %} {{ 'collection.general.page_size' | t: page_size: 36 }} {% render 'icon', icon: 'check-2' %} {{ 'collection.general.page_size' | t: page_size: 48 }} {% render 'icon', icon: 'check-2' %}
{{ 'collection.layout.title' | t }}

{%- if section.settings.default_view_layout == ‘grid’ -%}
{% render ‘icon’, icon: ‘grid’ %}
{% render ‘icon’, icon: ‘list’ %}
{%- else -%}
{% render ‘icon’, icon: ‘list’ %}
{% render ‘icon’, icon: ‘grid’ %}
{%- endif -%}

{%- comment -%}

COLLECTION PRODUCTS

{%- endcomment -%}

{%- if view_mode == ‘grid’ -%}
{%- assign show_as_list = false -%}
{%- else -%}
{%- assign show_as_list = true -%}
{%- endif -%}

{%- for product in search.results -%} {%- render 'product-item', product: product, list: show_as_list, grid_classes: grid_classes -%} {%- endfor -%}

{%- render ‘pagination’, paginate: paginate -%}

{%- endif -%}

{%- comment -%}
If the merchant has enabled the pages and articles, we allocate a space and an additional Ajax requests
will be done to fetch the results. Note that as we don’t know if there are page and/or blog results,
we hide the div by default
{%- endcomment -%}

{%- if settings.search_mode != ‘product’ -%}

{%- endif -%}
{%- endpaginate -%} {%- endif -%} {%- else -%}

{{ 'search.general.title' | t }}

{{ 'search.general.content' | t }}

{{ 'search.general.input_placeholder' | t }}

{% render ‘icon’, icon: ‘search’ %}

{{ ‘search.general.go_back_home’ | t }}

{%- endif -%}

{%- comment -%}

QUICK VIEW CONTAINER

{%- endcomment -%}

{%- render 'icon', icon: 'close' -%}
{%- render 'icon', icon: 'search-loader' -%}

{% schema %}
{
“name”: “Search page”,
“settings”: [
{
“type”: “select”,
“id”: “products_per_row”,
“label”: “Products per row (desktop)”,
“options”: [
{
“value”: “3”,
“label”: “3”
},
{
“value”: “4”,
“label”: “4”
}
],
“default”: “4”
},
{
“type”: “select”,
“id”: “default_view_layout”,
“label”: “Default view layout”,
“options”: [
{
“value”: “grid”,
“label”: “Grid”
},
{
“value”: “list”,
“label”: “List”
}
],
“default”: “grid”
},
{
“type”: “select”,
“id”: “show_quick_view”,
“label”: “Show quick view on…”,
“options”: [
{
“value”: “list”,
“label”: “List view”
},
{
“value”: “list_grid”,
“label”: “List and grid views”
}
],
“default”: “list”
},
{
“type”: “select”,
“id”: “show_quick_buy”,
“label”: “Show quick buy on…”,
“info”: “If product has variants, quick view and quick buy do the same thing.”,
“options”: [
{
“value”: “list”,
“label”: “List view”
},
{
“value”: “list_grid”,
“label”: “List and grid views”
}
],
“default”: “list”
},
{
“type”: “link_list”,
“id”: “quick_links”,
“label”: “Quick links”,
“info”: “This menu has limited support for dropdown items.”
}
]
}
{% endschema %}

search.content.liquid

{%- comment -%}
On the dedicated search results, we show products differently from blog posts/pages. As a consequence, this cause all
kind of issues when doing the pagination. To circumvent this issue, when we enter into the search results, we only show
products, and we do another Ajax request to load the products and blog posts only.
{%- endcomment -%}

{%- layout none -%}

{%- assign filtered_terms = search.terms | replace: ‘*’, ‘’ -%}

{%- if filtered_terms contains ’ AND ’ -%}
{%- assign filtered_terms = filtered_terms | split: ’ AND ’ | last -%}
{%- endif -%}

{%- if search.results_count > 0 -%}

{%- if search.types contains 'page' and search.types contains 'article' -%}

{{ 'search.general.pages_and_articles_with_terms' | t: terms: filtered_terms }}

{%- elsif search.types contains 'page' -%}

{{ 'search.general.pages_with_terms' | t: terms: filtered_terms }}

{%- else -%}

{{ 'search.general.articles_with_terms' | t: terms: filtered_terms }}

{%- endif -%}
    {%- for result in search.results limit: 20 -%} {%- if result.object_type == 'product' -%} {%- continue -%} {%- endif -%}
  • {{ result.title }}
  • {%- endfor -%}
{%- endif -%}

search.liquid

{% section ‘search-template’ %}
{% section ‘static-promotion-list’ %}

Hi @Calum375 ,

Please send me the code of search-template.liquid file, I will check it

{%- comment -%}We start by creating a lot of useful variables !{%- endcomment -%}

{%- assign view_mode = cart.attributes.collection_layout | default: section.settings.default_view_layout -%}
{%- assign filtered_terms = search.terms -%}

{%- assign items_per_page = cart.attributes.collection_products_per_page | default: 24 -%}

{%- if filtered_terms contains ’ AND ’ -%}
{%- assign filtered_terms = filtered_terms | split: ’ AND ’ | last -%}
{%- else -%}
{%- assign filtered_terms = filtered_terms | split: ‘product_type:’ | last -%}
{%- endif -%}

{%- assign quick_links_menu = linklists[section.settings.quick_links] -%}

{%- if quick_links_menu.links.size > 0 -%}
{%- assign has_filters = true -%}
{%- capture grid_classes -%}1/3–tablet-and-up 1/{{ section.settings.products_per_row }}–desk{%- endcapture -%}
{%- else -%}
{%- assign has_filters = false -%}
{%- capture grid_classes -%}1/3–tablet 1/{{ section.settings.products_per_row }}–lap-and-up{%- endcapture -%}
{%- endif -%}

{%- capture section_settings -%}
{
“currentSortBy”: “”,
“currentTags”: ,
“filterMode”: “”,
“defaultLayout”: {{ section.settings.default_view_layout | json }},
“defaultProductsPerPage”: 24,
“isAutomatic”: true,
“gridClasses”: {{ grid_classes | json }}
}
{%- endcapture -%}

{%- if search.performed and search.results_count > 0 -%}
  1. {{ 'general.breadcrumb.home' | t }} {%- render 'icon', icon: 'arrow-right' -%}
  2. {{ 'search.general.title_with_terms' | t: terms: filtered_terms }}
{%- endif -%}

{%- if search.performed -%}
{%- if search.results_count == 0 -%}

{{ 'search.general.title' | t }}

{{ 'search.general.no_results_with_terms' | t: terms: filtered_terms }}

{{ 'search.general.input_placeholder' | t }}

{% render ‘icon’, icon: ‘search’ %}

{{ ‘search.general.go_back_home’ | t }}

{%- else -%} {%- paginate search.results by items_per_page -%}
{%- if has_filters -%}
{%- if quick_links_menu.links.size > 0 -%}

{{ quick_links_menu.title }}

{%- endif -%}
{%- endif -%}
{%- if search.results_count > 0 -%}
{%- comment -%} IMPLEMENTATION NOTE: unfortunately I was forced to add a lot of extra div (collection__meta, collection__meta-inner, collection__header-inner,...) to be able to accommodate with the brand related layout which make things more complicated... {%- endcomment -%}

{{ 'search.general.products_with_terms' | t: terms: filtered_terms }}

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

{%- assign offset = paginate.current_offset | plus: 1 -%} {%- assign page_size = paginate.current_offset | plus: paginate.page_size | at_most: paginate.items -%}

{{ ‘search.general.showing_count’ | t: offset: offset, page_size: page_size, count: paginate.items }}

{{ ‘collection.general.display’ | t }}:

{{ 'collection.general.page_size' | t: page_size: paginate.page_size }}{%- render 'icon', icon: 'arrow-bottom' -%}
{% render 'icon', icon: 'nav-triangle-borderless' %}
{{ 'collection.general.display' | t }} {% render 'icon', icon: 'close' %}
{{ 'collection.general.page_size' | t: page_size: 24 }} {% render 'icon', icon: 'check-2' %} {{ 'collection.general.page_size' | t: page_size: 36 }} {% render 'icon', icon: 'check-2' %} {{ 'collection.general.page_size' | t: page_size: 48 }} {% render 'icon', icon: 'check-2' %}
{{ 'collection.layout.title' | t }}

{%- if section.settings.default_view_layout == ‘grid’ -%}
{% render ‘icon’, icon: ‘grid’ %}
{% render ‘icon’, icon: ‘list’ %}
{%- else -%}
{% render ‘icon’, icon: ‘list’ %}
{% render ‘icon’, icon: ‘grid’ %}
{%- endif -%}

{%- comment -%}

COLLECTION PRODUCTS

{%- endcomment -%}

{%- if view_mode == ‘grid’ -%}
{%- assign show_as_list = false -%}
{%- else -%}
{%- assign show_as_list = true -%}
{%- endif -%}

{%- for product in search.results -%} {%- render 'product-item', product: product, list: show_as_list, grid_classes: grid_classes -%} {%- endfor -%}

{%- render ‘pagination’, paginate: paginate -%}

{%- endif -%}

{%- comment -%}
If the merchant has enabled the pages and articles, we allocate a space and an additional Ajax requests
will be done to fetch the results. Note that as we don’t know if there are page and/or blog results,
we hide the div by default
{%- endcomment -%}

{%- if settings.search_mode != ‘product’ -%}

{%- endif -%}
{%- endpaginate -%} {%- endif -%} {%- else -%}

{{ 'search.general.title' | t }}

{{ 'search.general.content' | t }}

{{ 'search.general.input_placeholder' | t }}

{% render ‘icon’, icon: ‘search’ %}

{{ ‘search.general.go_back_home’ | t }}

{%- endif -%}

{%- comment -%}

QUICK VIEW CONTAINER

{%- endcomment -%}

{%- render 'icon', icon: 'close' -%}
{%- render 'icon', icon: 'search-loader' -%}

{% schema %}
{
“name”: “Search page”,
“settings”: [
{
“type”: “select”,
“id”: “products_per_row”,
“label”: “Products per row (desktop)”,
“options”: [
{
“value”: “3”,
“label”: “3”
},
{
“value”: “4”,
“label”: “4”
}
],
“default”: “4”
},
{
“type”: “select”,
“id”: “default_view_layout”,
“label”: “Default view layout”,
“options”: [
{
“value”: “grid”,
“label”: “Grid”
},
{
“value”: “list”,
“label”: “List”
}
],
“default”: “grid”
},
{
“type”: “select”,
“id”: “show_quick_view”,
“label”: “Show quick view on…”,
“options”: [
{
“value”: “list”,
“label”: “List view”
},
{
“value”: “list_grid”,
“label”: “List and grid views”
}
],
“default”: “list”
},
{
“type”: “select”,
“id”: “show_quick_buy”,
“label”: “Show quick buy on…”,
“info”: “If product has variants, quick view and quick buy do the same thing.”,
“options”: [
{
“value”: “list”,
“label”: “List view”
},
{
“value”: “list_grid”,
“label”: “List and grid views”
}
],
“default”: “list”
},
{
“type”: “link_list”,
“id”: “quick_links”,
“label”: “Quick links”,
“info”: “This menu has limited support for dropdown items.”
}
]
}
{% endschema %}

Hi @Calum375 ,

I checked and it includes pagination and filter, so if you hide with code, pagination will not work well. If you still want to change it, you can follow the instructions below:

Code:

{%- for product in search.results -%}
  {%- assign all_inventory = 0 -%}
  {%- for variant in product.variants -%}
    {%- if variant.inventory_management -%}
      {%- assign all_inventory = variant.inventory_quantity | at_least: 0 | plus: all_inventory -%}
    {%- endif -%}
  {%- endfor -%}
  {%- if all_inventory > 1 -%}
    {%- render 'product-item', product: product, list: show_as_list, grid_classes: grid_classes -%}
  {%- endif -%}
{%- endfor -%}

Best way, you should install app for it, refer:

Hope it helps!

Hi,

just curious if there was any way to add to this to prevent it coming up on google or while typing out the search. Thanks!