【发布时间】:2013-02-07 18:50:06
【问题描述】:
我正在尝试编写一个简单的 if 语句,但总是与 shopify 的系统斗争。
基本上我希望它这样做:
{% if collection.product == '停产' %} 该产品已停产。 {% endif %}
如果它在此集合中,则显示此文本/html。否则它不会显示任何东西。这将在 product.liquid 模板中。
有什么想法吗?
【问题讨论】:
标签: shopify
我正在尝试编写一个简单的 if 语句,但总是与 shopify 的系统斗争。
基本上我希望它这样做:
{% if collection.product == '停产' %} 该产品已停产。 {% endif %}
如果它在此集合中,则显示此文本/html。否则它不会显示任何东西。这将在 product.liquid 模板中。
有什么想法吗?
【问题讨论】:
标签: shopify
这就是最终的工作:
{% for c in product.collections %}
{% if c.handle == "discontinued" %}
This product is Discontinued
{% endif %}
{% endfor %}
【讨论】:
我想这对任何人都有帮助,我在 shopify 网站的侧边栏中使用过。 以下代码将检查当前集合页面。
<div class="row-fluid not-animated" data-animate="fadeInUp">
<div class="title">By Collections</div>
<form class="coll">
{% assign col_tags = collection.title %}
{% for collection in collections %}
<input type="radio" value="{{ collection.url }}" name="collections" {% if col_tags contains collection.title %} checked {% endif %} >{{ collection.title | escape }} <br/>
{% endfor %}
</form>
【讨论】:
如果我了解液体集合在 Shopify 中的工作原理,您将需要迭代所有产品。
如果您直接使用集合,则需要执行类似的操作:
{% for product in collection.product %}
{% if product.tags contains 'discontinued' %}
This product has been discontinued :(
{% endif %}
{% endfor %}
如果您只使用单个产品,您可能只使用内部if 液体标签部分。
参考资料:
【讨论】:
code: {% for product in collections['discontinued'].products %} This product has been discontinued :( {% endfor %} 但它试图显示停产产品的列表,而不仅仅是显示我的信息。不过,我仍在努力解决这个问题。
您确实可以将已停产的产品添加到名为已停产的集合中。
渲染产品时,您可以按照 csaunders 的建议进行操作,只需遍历已停产集合中的所有产品,并检查当前产品的 id 是否与该集合中的任何产品匹配。如果是这样,做你必须做的。无需使用标签。
【讨论】:
您可以使用product.collections 上的地图为产品创建一个集合数组。这将使用您指定的属性创建一个新数组,即每个集合的句柄。
然后您可以检查这个新数组 contains 是否是您要使用的句柄。
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'your-collection-handle' %}
{% comment %} DoSomething {% endcomment %}
{% endif %}
所以对于你的例子:
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'discontinued' %}
This product is Discontinued
{% endif %}
如果您的案例不同,您可以映射其他字段,例如标题。
【讨论】: