首页 > 解决方案 > Shopify 遗嘱主题更改变体以下拉选择如果超过 5 个产品

问题描述

我正在为一个有很多尺寸变体的项目对遗嘱主题进行一些更改。我无法将我的选择添加到购物车中。

在一个样本中显示 100 多个尺寸看起来很愚蠢。

如果产品的尺寸变体超过 10 个变体,我想将其更改为下拉列表。

{% if swatch contains 'size' or swatch contains 'Size' and swatch.size >= 2 %}

然后我想为这个项目创建一个下拉选择,通常会有一个样本。

我的选择

<select class="product-variants {{ value.name | handleize }} value-{{value.position}}"
                      id="SingleOptionSelector-{{ forloop.index0 }}"
                      data-index="option{{ forloop.index }}"
                    >
        {% for variant in product.variants %}
        {% assign value = variant.options[option_index] %}
        {% unless values contains value %}
        {% assign values = values | join: ',' %}
        {% assign values = values | append: ',' | append: value %} 
        {% assign values = values | split: ',' %}
         <option id="" value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
        {% endunless %}
      {% endfor %}
      </select>

我试图尽可能地镜像样本,这是样本

{% for variant in product.variants %}
    {% assign value = variant.options[option_index] %}
    {% unless values contains value %}
      {% assign values = values | join: ',' %}
      {% assign values = values | append: ',' | append: value %} 
      {% assign values = values | split: ',' %}
      <div data-value="{{ value | escape }}" class="swatch-element {% if is_color %}color {% endif %}{{ value | handle }} {% if variant.available %}available{% else %}soldout{% endif %}">
        {% if is_color %}
        <div class="tooltip">{{ value }}</div>
        {% endif %}
        <input id="swatch-{{ option_index }}-{{ value | handle }}" type="radio" name="option-{{ option_index }}" value="{{ value | escape }}"{% if forloop.first %} checked{% endif %} {% unless variant.available %}disabled{% endunless %} />
        {% if is_color %}
        <label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: {{ value | split: ' ' | last | handle }}; background-image: url({{ value | handle | append: '.' | append: file_extension | asset_url }})">
          <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
        </label>
        {% else %}
             <label for="swatch-{{ option_index }}-{{ value | handle }}">
              {{ value }}
              <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
             </label>
        {% endif %}
      </div>
    {% endunless %}
    {% if variant.available %}
    <script>
      jQuery('.swatch[data-option-index="{{ option_index }}"] .{{ value | handle }}').removeClass('soldout').addClass('available').find(':radio').removeAttr('disabled');
    </script>
    {% endif %}
  {% endfor %}
  {% endif %}

不幸的是,我选择的尺码没有放入购物车。如果我选择大 小 加入购物车。

这是我为本节创建的完整 if 语句

  {% if swatch contains 'size' or swatch contains 'Size' and swatch.size >= 2 %}
    <select class="product-variants {{ value.name | handleize }} value-{{value.position}}"
                      id="SingleOptionSelector-{{ forloop.index0 }}"
                      data-index="option{{ forloop.index }}"
                    >
        {% for variant in product.variants %}
        {% assign value = variant.options[option_index] %}
        {% unless values contains value %}
        {% assign values = values | join: ',' %}
        {% assign values = values | append: ',' | append: value %} 
        {% assign values = values | split: ',' %}
         <option id="" value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
         <input id="swatch-1-gold" type="radio" name="option-1" value="gold" checked>
        {% endunless %}
      {% endfor %}
      </select>

  {% else %}
  {% assign values = '' %}
  {% for variant in product.variants %}
    {% assign value = variant.options[option_index] %}
    {% unless values contains value %}
      {% assign values = values | join: ',' %}
      {% assign values = values | append: ',' | append: value %} 
      {% assign values = values | split: ',' %}
      <div data-value="{{ value | escape }}" class="swatch-element {% if is_color %}color {% endif %}{{ value | handle }} {% if variant.available %}available{% else %}soldout{% endif %}">
        {% if is_color %}
        <div class="tooltip">{{ value }}</div>
        {% endif %}
        <input id="swatch-{{ option_index }}-{{ value | handle }}" type="radio" name="option-{{ option_index }}" value="{{ value | escape }}"{% if forloop.first %} checked{% endif %} {% unless variant.available %}disabled{% endunless %} />
        {% if is_color %}
        <label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: {{ value | split: ' ' | last | handle }}; background-image: url({{ value | handle | append: '.' | append: file_extension | asset_url }})">
          <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
        </label>
        {% else %}
             <label for="swatch-{{ option_index }}-{{ value | handle }}">
              {{ value }}
              <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
             </label>
        {% endif %}
      </div>
    {% endunless %}
    {% if variant.available %}
    <script>
      jQuery('.swatch[data-option-index="{{ option_index }}"] .{{ value | handle }}').removeClass('soldout').addClass('available').find(':radio').removeAttr('disabled');
    </script>
    {% endif %}
  {% endfor %}
  {% endif %}
</div>

{% endunless %}

{% endif %}

这是完整的样本液体

{% assign file_extension = 'png' %}

{% if swatch == blank %}
<div class="swatch error">
  <p>You must include the snippet swatch.liquid with the name of a product option.</p> 
  <p>Use: <code>{% raw %}{% include 'swatch' with 'name of your product option here' %}{% endraw %}</code></p>
  <p>Example: <code>{% raw %}{% include 'swatch' with 'Color' %}{% endraw %}</code></p>
</div>
{% else %}

{% assign found_option = false %}
{% assign is_color = false %}
{% assign is_size = false %}

{% assign option_index = 0 %}

{% for option in product.options %}
  {% if option == swatch %}
    {% assign found_option = true %}
    {% assign option_index = forloop.index0 %}
    <style>
      label[for="product-select-option-{{ option_index }}"] { display: none; }
      #product-select-option-{{ option_index }} { display: none; }
      #product-select-option-{{ option_index }} + .custom-style-select-box { display: none !important; }
    </style>
    <script>$(window).load(function() { $('.selector-wrapper:eq({{ option_index }})').hide(); });</script>
    {% assign downcased_option = swatch | downcase %}
    {% if downcased_option contains 'color' or downcased_option contains 'colour' %}
      {% assign is_color = true %}
    {% elsif downcased_option contains 'size' %}
      {% assign is_size = true %}
    {% endif %}
  {% endif %}
{% endfor %}

{% unless found_option %}
<div class="swatch error">
  <p>You included the snippet swatch.liquid with the name of a product option — <code>'{{ swatch }}'</code> — that does not belong to your product.</p>
  <p>Use <code>{% raw %}{% include 'swatch' with 'name of your product option here' %}{% endraw %}</code></p>
  <p>Example: <code>{% raw %}{% include 'swatch' with 'Color' %}{% endraw %}</code></p>
  <p><strong>This is case-sensitive!</strong> Do not put in <code>'color'</code> if your product option name is <code>'Color'</code>.</p>
</div>
{% else %}
<div class="swatch clearfix" data-option-index="{{ option_index }}">
  <div class="header">{{ swatch }}</div>
  {% if swatch contains 'size' or swatch contains 'Size' and swatch.size >= 2 %}
    <select class="product-variants {{ value.name | handleize }} value-{{value.position}}"
                      id="SingleOptionSelector-{{ forloop.index0 }}"
                      data-index="option{{ forloop.index }}"
                    >
        {% for variant in product.variants %}
        {% assign value = variant.options[option_index] %}
        {% unless values contains value %}
        {% assign values = values | join: ',' %}
        {% assign values = values | append: ',' | append: value %} 
        {% assign values = values | split: ',' %}
         <option id="" value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
         <input id="swatch-1-gold" type="radio" name="option-1" value="gold" checked>
        {% endunless %}
      {% endfor %}
      </select>

  {% else %}
  {% assign values = '' %}
  {% for variant in product.variants %}
    {% assign value = variant.options[option_index] %}
    {% unless values contains value %}
      {% assign values = values | join: ',' %}
      {% assign values = values | append: ',' | append: value %} 
      {% assign values = values | split: ',' %}
      <div data-value="{{ value | escape }}" class="swatch-element {% if is_color %}color {% endif %}{{ value | handle }} {% if variant.available %}available{% else %}soldout{% endif %}">
        {% if is_color %}
        <div class="tooltip">{{ value }}</div>
        {% endif %}
        <input id="swatch-{{ option_index }}-{{ value | handle }}" type="radio" name="option-{{ option_index }}" value="{{ value | escape }}"{% if forloop.first %} checked{% endif %} {% unless variant.available %}disabled{% endunless %} />
        {% if is_color %}
        <label for="swatch-{{ option_index }}-{{ value | handle }}" style="background-color: {{ value | split: ' ' | last | handle }}; background-image: url({{ value | handle | append: '.' | append: file_extension | asset_url }})">
          <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
        </label>
        {% else %}
             <label for="swatch-{{ option_index }}-{{ value | handle }}">
              {{ value }}
              <img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
             </label>
        {% endif %}
      </div>
    {% endunless %}
    {% if variant.available %}
    <script>
      jQuery('.swatch[data-option-index="{{ option_index }}"] .{{ value | handle }}').removeClass('soldout').addClass('available').find(':radio').removeAttr('disabled');
    </script>
    {% endif %}
  {% endfor %}
  {% endif %}
</div>

{% endunless %}

{% endif %}

标签: shopifyshopify-template

解决方案


推荐阅读