Show the remaining inventory of a variant on product pages

You can add a message on the product page or featured product section that displays the number of items you have in stock when inventory runs low on a product variant. For this message to display, you need to activate inventory tracking for the product.

The steps for this customization vary depending on your theme. Click the button for your theme and follow the instructions.

Debut

Steps for Debut

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
<script>
  var variantStock = {};
</script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% form 'product'. Above this tag, add the following code:
<div class="inventoryWrapper">
  {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %}
    <p>Stock: {{ current_variant.inventory_quantity }}</p>
  {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{ current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit theme.js.liquid or theme.js

The changes that you need to make to these files depend on the version of Debut that you're using.

Versions 17.8.0 and below

  1. Open theme.js.liquid or theme.js.
  2. Find theme.Product = (function(). Below this.selectors = {, add the following code:
inventoryHtml: '.inventoryWrapper',
  1. In the same file, find _updateAddToCart: function(evt) {. Directly below, add the following code:
var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);
  1. Find if (variant.available) {. Before the } else { statement, add the following code:
if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) {
      const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
      inventoryWrapper.innerHTML = inventoryHtml;
    } else {
      inventoryWrapper.innerHTML = '';
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.

Versions 17.9.0 and above

  1. Open theme.js.liquid or theme.js.
  2. Find theme.Product = (function(). Below this.selectors = {, add the following code:
    inventoryHtml: '.inventoryWrapper',
  1. In the same file, find _setProductState: function(evt) {. Directly below, add the following code:
var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);
  1. In the same function, find if (!variant) {. After the closing } bracket, add the following code:
else {
  if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) {
    const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
    inventoryWrapper.innerHTML = inventoryHtml;
  } else {
    inventoryWrapper.innerHTML = '';
  }
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Brooklyn

Steps for Brooklyn

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
<script>
  var variantStock = {};
</script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% form 'product'. Above this tag, add the following code:
    <div class="inventoryWrapper">
      {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %}
    <p>Stock: {{ current_variant.inventory_quantity }}</p>
  {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit theme.js.liquid or theme.js

  1. Open theme.js.liquid or theme.js.
  2. Find theme.Product = (function(). Below this.selectors = {, add the following code:
inventoryHtml: '.inventoryWrapper',
  1. In the same file, find if (variant.available) {. Before the } else { statement, add the following code:
if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify') {
  const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
  $(this.selectors.inventoryHtml, this.$container).html(inventoryHtml);
} else {
  $(this.selectors.inventoryHtml, this.$container).html('');
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Simple

Steps for Simple

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
<script>
  var variantStock = {};
</script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% form 'product'. Above this tag, add the following code:
    <div class="inventoryWrapper">
      {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %}
    <p>Stock: {{ current_variant.inventory_quantity }}</p>
  {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit theme.js.liquid or theme.js

  1. Open theme.js.liquid or theme.js.
  2. Find theme.Product = (function(). Below this.selectors = {, add the following code:
inventoryHtml: '.inventoryWrapper',
  1. In the same file, find if (variant.available) {. Before the } else { statement, add the following code:
if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify') {
  const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
  $(this.selectors.inventoryHtml, this.$container).html(inventoryHtml);
} else {
  $(this.selectors.inventoryHtml, this.$container).html('');
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Minimal

Steps for Minimal

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
<script>
  var variantStock = {};
</script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% form 'product'. Above this tag, add the following code:
    <div class="inventoryWrapper">
      {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %}
    <p>Stock: {{ current_variant.inventory_quantity }}</p>
  {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit theme.js.liquid or theme.js

  1. Open theme.js.liquid or theme.js.
  2. Find theme.Product = (function(). Below this.selectors = {, add the following code:
inventoryHtml: '.inventoryWrapper',
  1. In the same file, find if (variant.available) {. Before the } else { statement, add the following code:
if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify') {
  const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
  $(this.selectors.inventoryHtml, this.$container).html(inventoryHtml);
} else {
  $(this.selectors.inventoryHtml, this.$container).html('');
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Boundless

Steps for Boundless

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
    <script>
  var variantStock = {};
    </script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% form 'product'. Above this tag, add the following code:
    <div class="inventoryWrapper">
      {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %}
        <p>Stock: {{ current_variant.inventory_quantity }}</p>
  {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit theme.js.liquid or theme.js

  1. Open ‘theme.js.liquid’ *or *‘theme.js’.
  2. Find theme.Product = (function(). Below var selectors = {, add the following code:
    inventoryHtml: '.inventoryWrapper',
  1. In the same file, find $(selectors.SKU, this.$container).html(variant.sku);. Directly below, add the following code:
    if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify') {
      const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`;
  $('.inventoryWrapper').html(inventoryHtml);
} else {
    $('.inventoryWrapper').html("");
}

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Narrative

Steps for Narrative

Edit theme.liquid

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit, and then click the ... button > Edit code.
  3. From the Layout directory, open theme.liquid.
  4. Find the closing </head> tag in the code. On a new line above the closing </head> tag, paste the following code:
    <script>
      var variantStock = {};
    </script>
  1. Click Save.
  1. From the Sections directory, open product-template.liquid or featured-product.liquid:
    • Open product-template.liquid to add this feature to product pages.
    • Open featured-product.liquid to add this feature to the featured product section on the home page.
  2. Find {% include 'product-form' %}. Above this tag, add the following code:
    <div class="inventoryWrapper">
      {% if current_variant.inventory_quantity > 0 and  current_variant.inventory_management == 'shopify' %}
        <p>Stock: {{ current_variant.inventory_quantity }}</p>
      {% endif %}
</div>

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include {{current_variant.inventory_quantity }} in your <p> tags.

  1. At the bottom of the file, add the following code:
    <script>
      {% for variant in product.variants %}
        variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
      {% endfor %}
    </script>
  1. Click Save.

Edit custom.js

  1. Open custom.js.
  2. At the bottom of the file, add the following code:
    var sections = window.theme.sections;
    var inventorySelectorHtml = '.inventoryWrapper';
    var productExtension = {
      init: function() {
          this.on('variant_change_successful', this._updateInventory.bind(this));
          this.on('variant_change_undefined', this._removeInventory.bind(this));
      },
      _removeInventory: function() {
    $(inventorySelectorHtml, this.container).html('');
      },
      _updateInventory: function(event, instance, product, variant) {
    if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify') {
      $(inventorySelectorHtml, this.container).html(`<p>Stock: ${variantStock[variant.id]}</p>`);
    } else {
      $(inventorySelectorHtml, this.container).html('');
    }
  }
};
    sections.extend('product-template', productExtension);
    sections.extend('featured-product', productExtension);

The code above outputs Stock: x. You can change the wording by adjusting the content in the <p> tags. Make sure to include ${variantStock[variant.id]} in your <p> tags.

  1. Click Save.
Can’t find the answers you’re looking for? We’re here to help.