Show/Hide Products With Zero Inventory

This wiki will go over how to add a show/hide product functionality for products that have 0 inventory left, and how to add it to the admin in the backend.

First, open your settings_schema.json, and create a new setting. Create something similiar to the code below.

 {
    "name": "Hide If Inventory is 0",
    "settings": [
       {
        "type": "select",
        "id": "hide_if_inventory_zero",
        "label": "Enable/Disable Hide",
        "options": [
          { 
            "value": "enabled", 
            "label": "Enable"
          },
          { 
            "value": "disabled", 
            "label": "Disable"
          }
        ]
      }
    ]
  },

If the setting is enabled, products with no inventory will be hidden. Otherwise the products will be displayed. Make sure the id is something unique to be called upon.

Next, open up your product-loop.liquid, and add in this logic at the top.

{% if settings.hide_if_inventory_zero == "enabled" %}
 {% if product-loop.available == true %}
At the bottom of product-loop.liquid, wrap up the file with this.
  {% endif %}
{% endif %}
Below the two endifs at the bottom page, add the beginning of last part of the logic below.
{% if settings.hide_if_inventory_zero == "disabled" %}
Copy and paste the normal content of product-loop.liquid underneath, and wrap up the logic with a closing endif.
{% endif %}
Your product-loop.liquid should now look something like this. And you're done!
{% if settings.hide_if_inventory_zero == "enabled" %}
  {% if product-loop.available == true %}


{% include '__product-image' %}
{% if settings.show-vendor-with-thumbnail == 'above-product-title' %}
{{ product-loop.vendor }}
{% endif %}

{{ product-loop.title }}

{% if settings.show-vendor-with-thumbnail == 'below-product-title' %}
{{ product-loop.vendor }}
{% endif %}
{% if has_prices and product-loop.price > 0 %}
{% if settings.show-compare-at-price and product-loop.compare_at_price > 0 %} {% capture was %} {% include '__localize' with 'was' %} {% if settings.show-currency-with-prices == "with-currency" %} {{ product-loop.compare_at_price_max | money_with_currency }} {% else %} {{ product-loop.compare_at_price_max | money }} {% endif %} {% endcapture %} {{ was | strip }}  {% elsif product-loop.price_varies %} {% include '__localize' with 'from' %} {% endif %} {% include '__poa-tag' with product-loop %} {% if price_is_poa %} {% include '__localize' with 'POA' %} {% else %} {% include 'product-price' with product-loop.price %} {% endif %} {% include 'product-labels' with product-loop %}
{% endif %} {% unless settings.yotpo-api-key == blank %}
{% endunless %} {% if settings.show-stock-availability-on-collection %}
{% include 'product-availability' with product-loop %}
{% if product-loop.metafields.inventory.ShappifySaleEndDate %}
{% include 'shappify-sales-clock' with product-loop %}
{% endif %}
{% endif %} {% if settings.quick-cart %}
 
{% if product-loop.variants.size > 1 %} {% if settings.collection-btn-icons %} {% else %} {% include '__localize' with 'Add to cart' %} {% endif %} {% else %} {% assign c_sum = 0.0 %} {% for v in product-loop.variants %} {% if v.price > 0 %} {% assign c_sum = c_sum | plus: v.price %} {% endif %} {% endfor %} {% if c_sum > 0 %} {% unless product-loop.template_suffix == 'quantity-limit' or product-loop.template_suffix == 'quantity-limit-1' or product-loop.template_suffix == 'quantity-limit-2' or product-loop.template_suffix == 'quantity-limit-3' or product-loop.template_suffix == 'quantity-limit-4' %}
{% endunless %} {% endif %} {% endif %}
{% endif %} {% if settings.quick-shop %} {% endif %}
 
{% endif %} {% endif %} {% if settings.hide_if_inventory_zero == "disabled" %}
{% include '__product-image' %}
{% if settings.show-vendor-with-thumbnail == 'above-product-title' %}
{{ product-loop.vendor }}
{% endif %}

{{ product-loop.title }}

{% if settings.show-vendor-with-thumbnail == 'below-product-title' %}
{{ product-loop.vendor }}
{% endif %}
{% if has_prices and product-loop.price > 0 %}
{% if settings.show-compare-at-price and product-loop.compare_at_price > 0 %} {% capture was %} {% include '__localize' with 'was' %} {% if settings.show-currency-with-prices == "with-currency" %} {{ product-loop.compare_at_price_max | money_with_currency }} {% else %} {{ product-loop.compare_at_price_max | money }} {% endif %} {% endcapture %} {{ was | strip }}  {% elsif product-loop.price_varies %} {% include '__localize' with 'from' %} {% endif %} {% include '__poa-tag' with product-loop %} {% if price_is_poa %} {% include '__localize' with 'POA' %} {% else %} {% include 'product-price' with product-loop.price %} {% endif %} {% include 'product-labels' with product-loop %}
{% endif %} {% unless settings.yotpo-api-key == blank %}
{% endunless %} {% if settings.show-stock-availability-on-collection %}
{% include 'product-availability' with product-loop %}
{% if product-loop.metafields.inventory.ShappifySaleEndDate %}
{% include 'shappify-sales-clock' with product-loop %}
{% endif %}
{% endif %} {% if settings.quick-cart %}
 
{% if product-loop.variants.size > 1 %} {% if settings.collection-btn-icons %} {% else %} {% include '__localize' with 'Add to cart' %} {% endif %} {% else %} {% assign c_sum = 0.0 %} {% for v in product-loop.variants %} {% if v.price > 0 %} {% assign c_sum = c_sum | plus: v.price %} {% endif %} {% endfor %} {% if c_sum > 0 %} {% unless product-loop.template_suffix == 'quantity-limit' or product-loop.template_suffix == 'quantity-limit-1' or product-loop.template_suffix == 'quantity-limit-2' or product-loop.template_suffix == 'quantity-limit-3' or product-loop.template_suffix == 'quantity-limit-4' %}
{% endunless %} {% endif %} {% endif %}
{% endif %} {% if settings.quick-shop %} {% endif %}
 
{% endif %}

Leave a comment