适合库存的Shopify Quantity下拉列表

时间:2014-10-03 23:37:01

标签: javascript html css shopify liquid

我有一家shopify商店。如何匹配数量下拉列表以匹配库存中的实际数量。例如,如果有3个项目,则数量下拉列表的最大值应为3.如果有5个项目,则最大值应为5.

谢谢, 亚历

2 个答案:

答案 0 :(得分:0)

如果您想在产品页面中添加数量下拉菜单,可以使用page in the Shopify docs向您展示如何执行此操作:

<label for="quantity">Qty: </label>
<select id="quantity" name="quantity">
{% for i in (1..10) %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>

(注意:请确保使用name="quantity",以便将值传递给购物车。)

根据剩余库存设置最大数量的问题是数量是variants的属性,而不是产品。如果您只有一个变体,如果您将上面的for循环改为:

,它将正常工作
{% for i in (1..product.variants.first.inventory_quantity) %}

否则,最好限制购物车中的数量。在cart.liquid中试试这个:

<select id="updates_{{ item.id }}" name="updates[]"> 
{% for i in (1..item.variant.inventory_quantity) %}
<option value="{{ i }}"{% if item.quantity == i %} selected{% endif %}>{{ i }}</option>
{% endfor %}
</select>

答案 1 :(得分:0)

I moved this functionality to jQuery and removed the liquid from the product page. In other words, I was using an implementation like the one mentioned by Steph Sharp, but came up against the restriction she mentions. My quantity select element is now empty:

<select id="quantity" name="quantity"></select>

Using jQuery, I implemented the quantity dropdown that tracks stock thusly:

for(var counter = 1; counter <= variant.inventory_quantity; counter++){
  if(counter === 1){
    $('#quantity').empty();
  }
  var newQtyOption = $("<option></option>").val(counter).html(counter);
  $('#quantity').append("<option value='" + counter + "'>" + counter + "</option>")
}

This clears the select element and then adds a child option element for each quantity for this variant. This was built up from Caroline Schnapp's code https://gist.github.com/carolineschnapp/1083007