如何解决检查选项是否可用的简便方法
我正在寻找一种方法来轻松检查某个选项是否可用(未售完),因为我使用的主题会在产品上的选项之间循环,然后显示色板。
我当前的解决方案效率低下,我想找到一种更有效的解决方法。
下面是产品模板
{% 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 举报,一经查实,本站将立刻删除。