Inventory count of certain variants in a collection

Hi all

 

I'm currently attempting to calculate and display the combined inventory of certain variants in a collection.

 

Basically I design and make bags and offering a limited number of a custom product that is available in four sizes.

 

http://www.mattt.com.au/collections/ttthings

 

I have the first product option set up as the fabrics, so I want to only use the variants with a fabric description that contains "Custom", which is also the first variant in each product though may not always be the case.

 

So I'd like to get the inventory value for these variants from the four products, add them together and display them as an announcement like "35 of 50 limited edition products available", and when these products are bought the count goes down.

 

I've tried using the code:

 

{% assign counttotal = 0 %}
{% for product in collections.ttthings.products %}
  	{% if product.variant.option1 contains 'Custom' %}  
		 {% capture customcount %}{{ product.variant.inventory_quantity }}{% endcapture %}
                {% capture tempcount %}{{ counttotal | plus: customcount }}{% endcapture %} 
    {% endif %}
    {% assign counttotal = tempcount %}
{% endfor %}
{% assign ttthing_count = counttotal | plus: 0 %}

Though not getting any result at the moment ....

 

I suspect I've done something very minor that is throwing things off.

 

Any suggestions would be greatly appreciated :) 

 

Matt

0 Likes

Hi all

 

After much fooling around I worked it out:

 

{% assign counttotal = 0 %}
{% for product in collections.ttthings.products %}  
	{% for variant in product.variants %}
		{% if variant.option1 contains 'Custom' %}
		      {% capture customcount %}{{ variant.inventory_quantity | times: -1 }}{% endcapture %}
        	      {% capture tempcount %}{{ counttotal | plus: customcount }}{% endcapture %} 
		{% endif %}
    		{% assign counttotal = tempcount %}
	{% endfor %}
{% endfor %}
{% assign ttthing_count = counttotal | plus: 0 %}

Thought I'd post this here in case anyone was interested.

0 Likes