2014-12-04 57 views
0

我试图做类似这样的问题的东西:Shopify购物车 - 显示项基于标题

Shopify - If item in cart belongs to a specific collection

,我已经试过编辑代码,以满足我的需求,但它似乎增加一倍输出。

我要的是检查,看是否有购物车中的商品属于特定集合(或检查标题),然后下方显示相关项目。

我当前的代码是:

{% for item in cart.items %} 

    {% for collection in item.product.collections %} 
     {% if collection.handle == "mukluks" %} 
      this is a mukluk 
     {% endif %} 
    {% endfor %}    
{% endfor %} 

但是输出“这是一个mukluk”每有一个比赛的时间。我仍然试图找出如何将它限制为一个。也许用forloop?

回答

1

您的方法是正确的,而且非常类似于我的建议。或者,你可以使用if语句和两个条件:

{% assign found_mukluk = false %}    
{% for item in cart.items %} 
    {% for collection in item.product.collections %} 
     {% if found_mukluk == false and collection.handle == "mukluks" %} 
      {% assign found_mukluk = true %} 
      this is a mukluk 
     {% endif %} 
    {% endfor %} 
{% endfor %} 
+0

这对我来说似乎更有效率,所以我将它标记为正确答案而不是我的。 – Patrick 2014-12-05 18:23:45

0

好吧,我最终搞清楚了这一点。我只是将一个变量赋值为false,然后在循环中匹配它。请,如果您有更好或更有效的解决方案,请告诉我。

{% assign found_mukluk = false %}    
{% for item in cart.items %} 


    {% for collection in item.product.collections %} 

     {% if collection.handle == "mukluks" %} 
      {% assign found_mukluk = true %} 
     {% endif %} 
    {% endfor %} 

{% endfor %} 
{% if found_mukluk %} 
      this is a mukluk 
{% endif %}