【问题标题】:Shopify: Hide size variant heading in liquid template when there are no sizes definedShopify:当没有定义尺寸时,在液体模板中隐藏尺寸变体标题
【发布时间】:2018-09-14 11:58:33
【问题描述】:

我正在使用 Shopify,当没有为产品定义尺寸时,我想在模板中隐藏尺寸变量标题。

这是我商店中渲染代码的屏幕截图。

这是我的液体模板中的代码 sn-p:

<div class="options clear">
{% if product.available %}
<div class="product-info-row">
  <div class="product-info-col1">
    <h4>Quantity</h4>
    <div class="quantity">
      <div class="quantity-display">1</div>
      <input name="quantity" type="hidden" value="1" data-max="{{product.selected_or_first_available_variant.inventory_quantity}}">
      <div class="quantity-decrement">-</div>
      <div class="quantity-increment">+</div>
    </div>
  </div>
  {% if product.variants.size >= 1 %}
  <div class="product-info-col2">
    <h4>Size</h4>
    <div class="variants">
      {% unless product.selected_or_first_available_variant.title contains "Default" %}
      <ul class="variant-list">
        {% for variant in product.variants %}
        {% if variant.available %}
        <li class="{% if variant == product.selected_or_first_available_variant %} selected{% endif %}" data-value="{{ variant.id }}" data-quantity="{{ variant.inventory_quantity }}"> {{ variant.title }}</li>
        {% endif %}
        {% endfor %}
      </ul>
      {% endunless %}
    </div>
  </div>
  {% endif %}
  <input type="hidden" class="variant-id" name="id" value="{{product.selected_or_first_available_variant.id}}">
</div>
{% endif %}

我有点困惑,因为在 HTML 中已经有针对尺寸变体的运算符逻辑:{% if product.variants.size &gt;= 1 %}。该产品没有定义尺寸的变体(0 小于 1),那么,为什么这个尺寸变体的 HTML 块仍然显示?任何帮助将非常感激。谢谢!

【问题讨论】:

    标签: shopify liquid shopify-template


    【解决方案1】:

    Shopify 中的每个产品都至少包含一个变体。您的条件{% if product.variants.size &gt;= 1 %} 为真。但是{% unless product.selected_or_first_available_variant.title contains "Default" %} 是假的。

    为了更好地理解产品对象,我建议您将{{ product | json }} 添加到您的液体中,以将所有对象呈现为序列化的 JSON。它将使您更好地了解产品对象及其领域

    【讨论】:

    • 非常感谢。只需将{% unless product.selected_or_first_available_variant.title contains "Default" %} 向上移动几行就可以满足我的需要。
    • 是的,你说得对。这将解决你的问题。但是如果您将来会感到困惑,我仍然建议您查看产品对象
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多