【发布时间】:2020-04-20 05:44:05
【问题描述】:
此代码有问题。 我想做的是:
如果产品已经在购物车中 >
显示一次“查看购物车”按钮。
其他
显示添加购物车 btn 一次
确实如此,但它打印出来的就像单个产品的数量(循环进入)是 5,所以它重复添加到购物车 btn 5 次。 无论如何,我的代码在这里:
{% for product in collections[collection_].products %}
<div class="single-product">
<div class="single-product-image"><img src="{{ product.featured_image.src | img_url: 'large' }}" alt="{{ product.featured_image.alt | escape }}"></div>
<div class="single-product-details">
<h3><a href="{{ product.url | within: collection }}">{{ product.title }}</a></h3>
<div class="product-price">
<span class="new-price">{{ product.price | money }}</span>
</div>
</div>
<div class="single-product-action">
{% for item in cart.items %}
{% if item.product.id == product.id %}
<a href="/cart" class="added_to_cart">View Cart</a>
{% else %}
{% assign not_in_cart = true %}
{% endif %}
{% endfor %}
{% if not_in_cart %}
Add to Cart btn
{% endif %}
</div>
</div>
{% endfor %}
我需要它输出:
如果产品在购物车中,则查看购物车 btn 一次 或者 如果产品不在购物车中,则添加到购物车 btn 谢谢
【问题讨论】:
-
我在 Debut 主题上尝试了
{% for item in cart.items %}循环中的代码,但按钮只显示了一次。代码中可能缺少其他内容,但我想知道 {% break %} 标签是否适合您? -
谢谢!解决了!