<form action="/cart/add" method="post" style="text-align:center;">
<select name="id" id="{{ product.handle }}" style="display: none;">
{% for variant in product.variants %}
{% if variant.available %}
<option value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>
{% else %}
<option disabled="disabled">{{ variant.title }} - Sold Out</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="Add to cart" class="btn" />
</form>
{{ 'option_selection.js' | shopify_asset_url | script_tag }}
<script>
var all_products = { {% for product in collection.products %}'{{ product.handle }}': {{ product | json }},{% endfor %} };
for(curr_product in all_products){
new Shopify.OptionSelectors(curr_product, {
product: all_products[curr_product]
});
}
</script>
我们依靠购物功能
new Shopify.OptionSelectors
它将把每个选择分成一个单独的选择。别忘了加上
id="{{ product.handle }}"
到主选择。
{%- for product in collection.products -%}
<form action="/cart/add" method="post" style="text-align:center;">
<select name="id" id="{{ product.handle }}" style="display: none;">
{% for variant in product.variants %}
{% if variant.available %}
<option value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>
{% else %}
<option disabled="disabled">{{ variant.title }} - Sold Out</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="Add to cart" class="btn" />
</form>
{% endfor %}
{{ 'option_selection.js' | shopify_asset_url | script_tag }}
<script>
var all_products = { {% for product in collection.products %}'{{ product.handle }}': {{ product | json }},{% endfor %} };
for(curr_product in all_products){
new Shopify.OptionSelectors(curr_product, {
product: all_products[curr_product]
});
}
</script>