2014-12-04 52 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 %}