Skip to content

Instantly share code, notes, and snippets.

@kyleaparker
Last active December 27, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleaparker/7360715 to your computer and use it in GitHub Desktop.
Save kyleaparker/7360715 to your computer and use it in GitHub Desktop.
Multi variant add to cart form
<div id="inventory">
<h3>Inventory Available</h3>
{% assign options1 = product.variants | map: "option1" %}
<table>
<tr>
{% if product.options.size > 1 %}<th>&nbsp;</th>{% endif %}
{% assign options1Filtered = '' %}
{% for option1 in options1 %}
{% assign option1Space = option1 | prepend: ', ' %}
{% unless options1Filtered contains option1Space %}
<th><strong>{{ option1 }}</strong></th>
{% assign options1Filtered = options1Filtered | append: ', ' | append: option1 %}
{% endunless %}
{% endfor %}
</tr>
{% if product.options.size > 1 %}
{% assign options2 = product.variants | map: "option2" %}
{% assign options2Filtered = '' %}
{% assign options1Split = options1Filtered | remove_first: ', ' | split: ', ' %}
{% assign index = 1 %}
{% for option2 in options2 %}
<tr>
{% assign optionSpace = option2 | prepend: ', ' %}
{% unless options2Filtered contains optionSpace %}
<td><strong>{{ option2 }}</strong></td>
{% for option1 in options1Split %}
{% assign variantFound = false %}
<td>
{% for variant in product.variants %}
{% if variant.option1 == option1 and variant.option2 == option2 %}
{% if variant.available %}<input type="text" class="quantity-add" name="quantity-{{ forloop.index }}" id="{{ variant.id }}" value="0" size="2">{% else %}N/A{% endif %}
{% assign variantFound = true %}
{% endif %}
{% if forloop.last and variantFound == false %}N/A{% endif %}
{% endfor %}
</td>
{% endfor %}
{% assign options2Filtered = options2Filtered | append: ', ' | append: option2 %}
{% endunless %}
</tr>
{% endfor %}
{% else %}
<tr>
{% for variant in product.variants %}
<td><small>{{ variant.inventory_quantity }} Available</small><br><input type="text" class="quantity-add" name="quantity-{{ forloop.index }}" id="{{ variant.id }}" value="0" size="2">
</td>
{% endfor %}
</tr>
{% endif %}
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment