How to Display Variant Inventory Quantities on Shopify

Published on Aug 18, 2024

By Michael Chen

#Shopify#E-commerce#Inventory Management
Man in White Shirt Standing Beside Woman in White Shirt

In the world of e-commerce, providing accurate inventory information to customers is crucial. Shopify store owners often face challenges when it comes to displaying the inventory quantity for different product variants. This guide will walk you through a step-by-step process to show inventory quantities for each variant, even after the deprecation of the variant.inventory_quantity variable.

Understanding the Challenge

The Deprecation of variant.inventory_quantity

Shopify recently deprecated the variant.inventory_quantity variable, which previously made it simple to display inventory levels. This change has left many store owners searching for alternative solutions.

The Need for Accurate Inventory Display

Showing accurate inventory levels can create urgency and help customers make informed decisions. It’s a valuable feature that can potentially boost sales and improve user experience.

Compatibility Considerations

Before diving into the solution, it’s important to note that this guide assumes you’re using a theme that supports jQuery. If your theme doesn’t include jQuery, you’ll need to adapt the provided code to vanilla JavaScript.

The Solution: A Step-by-Step Approach

Step 1: Locating the Product Template File

First, navigate to your theme files and locate the product.liquid or product-template.liquid file. This file contains the HTML code for your product pages.

Step 2: Adding the Inventory Display HTML

In the product template file, find the section where you want to display the inventory quantity. Insert the following line of code:

<B id="variant-inventory">{{ current_variant.inventory_quantity }}</B>

You can customize this line to fit your design preferences, but ensure you keep the id="variant-inventory" attribute intact.

Step 3: Handling Variant Changes

For products with variants, you’ll need to update the inventory display when a customer selects a different option. This requires modifying your theme’s JavaScript file.

Locating the Variant Change Function

Open your theme’s JavaScript file (often named theme.js) and find the function that handles variant changes. In the Debut theme, this is typically the _onSelectChange function.

Adding the Update Function

Insert the following function into your JavaScript file:

function updateInventoryQuantity(variant) {
  if (variant) {
    setTimeout(function() {
      $('#variant-inventory').text(variant.inventory_quantity);
    }, 100);
  }
}

Calling the Update Function

Within the variant change handler, call the updateInventoryQuantity function, passing the current variant as a parameter.

Implementing the Solution

For Single-Variant Products

If your product doesn’t have variants, the HTML addition in Step 2 should be sufficient to display the inventory quantity.

For Multi-Variant Products

For products with multiple variants, you’ll need to ensure the JavaScript function is called whenever a variant is selected. This typically involves modifying your theme’s existing variant selection code.

Troubleshooting Common Issues

Delayed Updates

The setTimeout function in the JavaScript code introduces a slight delay to ensure the inventory quantity updates after the variant selection is processed. If you’re experiencing issues, you may need to adjust this delay.

CSS Styling

Some users have reported alignment issues with the inventory display. To resolve this, you may need to add custom CSS to your theme to ensure proper alignment with other product information.

Advanced Customizations

Displaying “10+” for Large Quantities

For stores that prefer not to show exact quantities above a certain threshold, you can modify the JavaScript function to display “10+” for quantities exceeding 10:

function updateInventoryQuantity(variant) {
  if (variant) {
    setTimeout(function() {
      var quantity = variant.inventory_quantity;
      $('#variant-inventory').text(quantity > 10 ? '10+' : quantity);
    }, 100);
  }
}

Hiding Inventory Display for Specific Products

If you need to hide the inventory display for certain products (e.g., gift cards), you can add conditional logic to your product template:

{% unless product.gift_card? %}
  <B id="variant-inventory">{{ current_variant.inventory_quantity }}</B>
{% endunless %}

Considerations for Different Themes

Adapting to Theme-Specific Code

While this guide uses the Debut theme as an example, the principles can be applied to other Shopify themes. You may need to adjust the code slightly based on your theme’s structure and existing JavaScript.

Using the Shopify API for Complex Scenarios

For more complex inventory management scenarios, consider leveraging the Shopify API. The Product Availability feature (https://shopify.dev/themes/delivery-fulfillment/pickup-availability) can be customized to provide detailed inventory information across various contexts.

By following this guide, you should be able to successfully display variant inventory quantities on your Shopify store, enhancing the shopping experience for your customers. Remember to test thoroughly across different product types and variants to ensure consistent functionality.

Take Our Quick Quiz:

Which primary product image do you think has the highest conversion rate?