【问题标题】:Get one variant in Shopify在 Shopify 中获取一种变体
【发布时间】:2014-06-18 16:07:42
【问题描述】:

我的产品有 2 种款式,尺寸和颜色。我想将尺寸选项显示为一系列按钮。这是我当前的代码:

{% for variant in product.variants %}
  <input type="button" name="{{ variant.id }}" id="{{ variant.id }}" value="{{ variant.title }}">
{% endfor %}

这将返回具有 S/White、M/White、L/White 等值的按钮。我只想要 S、M 和 L。从文档中的示例代码中提取,我已经尝试过

{% for variant in product.variants.size %}

{% for variant in product.variants.first %}

但显然这不是正确的语法,因为没有任何输出。

这样做的正确方法是什么?

TIA - 乔

【问题讨论】:

    标签: shopify variants


    【解决方案1】:

    变体最多包含 3 个选项。在您的情况下,variant.option1 会给您尺寸(S/M/L),variant.option2 是颜色。您还可以使用product.options 获取选项的标题。 See the doco here 了解有关变体的更多信息。

    另外,您是否看过 this tutorial 向产品页面添加色板和按钮?也许一些用于创建按钮的代码可以帮助您入门。

    编辑:

    按照上面提到的教程,你可以像这样获得尺寸选项的按钮:

    将此代码放在&lt;/select&gt;下方的product.liquid中:

    {% if product.available and product.variants.size > 1 %}
      {% include 'swatch' with 'Size' %}
    {% endif %}
    

    如果您还需要颜色选项按钮(而不是色板),请在 product.liquid 中使用此代码:

    {% if product.available and product.variants.size > 1 %}
      {% for option in product.options %}
        {% include 'swatch' with option %}
      {% endfor %}
    {% endif %}
    

    并从 swatch.liquid 中删除这些行(第 30 行):

    {% if downcased_option contains 'color' or downcased_option contains 'colour' %}
      {% assign is_color = true %}
    {% endif %}
    

    【讨论】:

    • 感谢您的回复,Steph,但我无法集成 .option1 并获得我正在寻找的结果。我尝试了&lt;input type="button" name="{{ variant.option1.id }}" id="{{ variant.option1.id }}" value="{{ variant.option1.title }}"&gt;{% for variant in product.variants.option1 %},但都没有奏效。你能给我正确的语法吗?而且,是的,我熟悉那个教程——我已经开始工作了;我将更深入地查看代码,看看我能发现什么。
    • @JoeLowery 请参阅我上面的编辑,了解如何使用色板教程使按钮工作。这可能是最简单的方法。
    • 非常感谢,斯蒂芬。很抱歉我延迟接受您的回答。效果很好。
    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2019-07-10
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多