代码之家  ›  专栏  ›  技术社区  ›  Igor Voytt

在收藏页shopify中显示变体选项

  •  1
  • Igor Voytt  · 技术社区  · 6 年前

    所以我用这段代码在我的收藏中显示变体,然后添加到购物车中。

    <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主题是首次亮相,如果它有帮助的话。

    0 回复  |  直到 6 年前
        1
  •  0
  •   drip    6 年前

    <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>