检查选项是否可用的简便方法

问题描述

我正在寻找一种方法来轻松检查某个选项是否可用(未售完),因为我使用的主题会在产品上的选项之间循环,然后显示色板。

我当前的解决方案效率低下,我想找到一种更有效的解决方法

下面是产品模板

{% unless hide_default_title %}
    {% if section.settings.product_variant_style == 'buttons' or section.settings.product_variant_style == 'swatches' %}
        <div class="product-single__swatches">                                                     
            {% for option in product.options_with_values %}
                 {% include 'product-swatch' style: section.settings.product_variant_style %}
            {% endfor %}
        </div>
    {% endif %}
{% endunless %}

下面是产品样本文件的一部分,我需要确定某个选项是否可用,我的解决方案是循环浏览产品的所有变体,然后循环浏览变体的所有选项,然后最后检查variant选项和该选项的值是否相同以及该选项是否可用

{% comment %} if color swatch {% endcomment %}
{% if color_swatch_options contains option_name and style == 'swatches' %}
    <div class="product-single__swatch__item product-single__swatch__item--color js-swatch-color-item">
         <input type="radio" class="product-single__swatch__input u-hidden-visually" id="{{ id }}" name="{{ option_name }}-c" value="{{ value | escape }}" data-position="{{ option.position }}" {{ checked }}/>
             <label for="{{ id }}" class="product-single__swatch__label">
             <span class="product-single__swatch__label__graphic{% if color_name == 'white' %} product-single__swatch__label__graphic--white{% endif %}" style="background-color: {{ swatch_css_color }};{% if section.settings.use_swatch_images %}background-image: url({{ image_url }}){% endif %}"></span>
             <span class="u-hidden-visually">{{ value }}</span>
         </label>
    </div>
{% comment %} if regular swatch {% endcomment %}
{% else %}
     <div class="product-single__swatch__item">
         {% for variant in product.variants %}
             {% for temp in variant.options %}
                 {% if temp == value and variant.available == false %}
                     <input type="radio" class="product-single__swatch__input u-hidden-visually" id="{{ id }}" name="{{ option_name }}-b" value="{{ value | escape }}" data-position="{{ option.position }}" {{ checked }} />
                     <label for="{{ id }}" class="product-single__swatch__label sold-out"  onclick='return false;'>{{ value }}</label>
                 {% elsif temp == value %}
                     <input type="radio" class="product-single__swatch__input u-hidden-visually" id="{{ id }}" name="{{ option_name }}-b" value="{{ value | escape }}" data-position="{{ option.position }}" {{ checked }} />
                     <label for="{{ id }}" class="product-single__swatch__label">{{ value }}</label>
                {% endif %}
            {% endfor %}
        {% endfor %}
    </div>
{% endif %}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)