2014-10-03 46 views
1

我有一個shopify商店。如何匹配數量下拉以匹配庫存中的實際數量。例如,如果有3個項目,數量下拉應具有3的最大值。如果有5個項目,然後最大值應該是5Shopify數量下拉,適合庫存

謝謝, 亞歷克斯

回答

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"所以價值傳遞給車)

問題無線th根據剩餘庫存設置最大數量是數量是屬性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> 
0

我把這個功能移動到jQuery並從產品頁面中刪除液體。換句話說,我使用的是類似Steph Sharp所提到的實施方式,但遭到了她提到的限制。我的數量select元素現在是空的:

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

使用jQuery,我實施了正是如此跟蹤的股票數量下拉:

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>") 
} 

這將清除select元素,然後將子option元素每個數量對於這個變體。這是從Caroline Schnapp的代碼https://gist.github.com/carolineschnapp/1083007