【发布时间】:2021-08-24 16:37:47
【问题描述】:
我已将product_options.js 加载到我的商店中,我希望在用户选择变体时更改数量下拉列表的最大值。我有一个数量下拉菜单,虽然它不会根据库存水平而改变。
这是运行变体选择器的代码:
<script>
// <![CDATA[
var selectCallback = function(variant, selector) {
if (variant) {
if (variant.available) {
// Selected a valid variant that is available.
$('#add-to-cart-button').removeClass('disabled').removeAttr('disabled').html('Add to Cart');
} else {
// Variant is sold out.
$('#add-to-cart-button').html('Sold Out').addClass('disabled').attr('disabled', 'disabled');
}
// Whether the variant is in stock or not, we can update the price and compare at price.
if ( variant.compare_at_price > variant.price ) {
$('#price-field').html('<span class="product-price on-sale">'+ Shopify.formatMoney(variant.price, "{{ shop.money_format }}") +'</span>'+' <s class="product-compare-price">'+Shopify.formatMoney(variant.compare_at_price, "{{ shop.money_format }}")+ '</s>');
} else {
$('#price-field').html('<span class="product-price">'+ Shopify.formatMoney(variant.price, "{{ shop.money_format }}") + '</span>' );
}
{% if product.variants.size == 1 and product.variants.first.title contains 'Default' %}
$('.selector-wrapper').hide();
{% endif %}
} else {
// variant doesn't exist.
$('#add-to-cart-button').val('Unavailable').addClass('disabled').attr('disabled', 'disabled');
}
}
// initialize multi selector for product
jQuery(function($) {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }} , onVariantSelected: selectCallback });
});
// ]]>
</script>
产品页面上的液体。
<select id="product-select" name="id">
{% for variant in product.variants %}
<option value="{{ variant.id }}"
{% if variant == product.selected_or_first_available_variant %}selected="selected"{% endif %}
>
{{ variant.title }} - {{ variant.price | money }}
</option>
{% endfor %}
</select>
<label for="quantity">Qty: </label>
<select id="quantity" name="quantity">
{% for i in (1..10) %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
是否可以根据变体更改回调更改 i 值?
【问题讨论】: