在集合页面 Shopify 中显示变体选项

2024-03-09

我知道这个问题在互联网上被问了数百万次,但似乎每个人都希望有一个有自己特色的解决方案。我找不到我真正需要的东西。

因此,我使用此代码来显示我的收藏中的变体,然后添加到购物车。

<form action="/cart/add" method="post" style="text-align:center;">
   
  <select name="id">
  {% 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>

这有效,但在下拉菜单中,它给了我这样的结果:

xs / Black - $72.00     
small / Black - $61.00     
medium / Black - $52.00     
large / Black - $74.00     
xl / Black - $77.00     
xxl / Black - $55.00     
xs / Blue - $72.00    
small / Blue - $72.00     
medium / Blue - $72.00     
xl / Blue - $72.00    
xxl / Blue - $72.00    

我想要的是客户在不同的下拉列表中分别选择尺寸和颜色,然后单击“添加到购物车”。

我到处寻找如何做到这一点但没有运气。请帮忙。 如果有帮助的话,我的 Shopify 主题是 Debut。


要在单独的块中获取它,您必须迭代产品选项(最多 3 个),以便您可以单独显示尺寸和颜色。

{% unless product.has_only_default_variant %}
{% for option in product.options_with_values %}
<div class="selector-wrapper js product-form__item">
<label {% if option.name == 'default' %}class="label--hidden" 
{% endif %}for="SingleOptionSelector-{{ forloop.index0 }}">
                {{ option.name }}
              </label>
<select style="display:block" class="single-option-selector single-option-selector-{{ section.id }} product-form__input" id="SingleOptionSelector-{{ forloop.index0 }}" data-index="option{{ forloop.index }}">
{% for value in option.values %}
<option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
{% endfor %}
</select>
</div>
{% endfor %}
{% endunless %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在集合页面 Shopify 中显示变体选项 的相关文章

随机推荐