Quantcast
Viewing all articles
Browse latest Browse all 12

How to display current inventory numbers for each variant of a product in Shopify

This is just another little code snippet that you might find useful in Shopify. You can show when a product is unavailable due to being out-of-stock by placing something like the following pseudocode into your product.liquid template file:

{% if product.available %}
 ... whatever you want to show here for available products...
{% else %}
 ... whatever you want to show for unavailable (out-of-stock) products...
{% endif %}

But what if you want to give your customers a little more detail than that? Maybe you want to let people know the exact quantity of a certain product that is left in inventory. You can do that with something like the code below (this also would go in your product.liquid template file):

<p><em>Our inventory currently shows the following quantities available for this product:</em></p>
<ul>
{% for variant in product.variants %}
<li><em>{{ variant.title}} : {{ variant.inventory_quantity  }}</em></li>
{% endfor %}
</ul>

This code shows the quantity left for each variant of a product. So if the product is a t-shirt that comes in small, medium, or large, the code above will produce a list that shows the number of smalls left, the number of mediums left, and the number of larges left.

UPDATE 11/1/2014: In the comments for this post, James asked if there is a way to not display anything for products that have inventory tracking turned off. I did a little experimenting with that and based on my testing the following code will make it so the products variants that are set to “Don’t track inventory” will just not show up in the inventory list:

<p><em>Our inventory currently shows the following quantities available for this product:</em></p>
<ul>
{% for variant in product.variants %}
{% if variant.inventory_management == 'shopify' %}
<li><em>{{ variant.title}} : {{ variant.inventory_quantity  }}</em></li>
{% endif %}
{% endfor %}
</ul>

This last part (under update) I have not done a lot of testing on so if anybody knows of a reason this code would be problematic or a better way to do it, please feel free to say so in the comments.


Viewing all articles
Browse latest Browse all 12

Trending Articles