项目购物车价格和比较价格复制

Item cart price and compare price duplicating

如果我有不止一种变体的价格并且比较价格重复,假设我有黑色和高的变体,则每个变体旁边两次是 200 美元。

如何只显示一次?

这是我的代码:

{% assign beforePrice = priceCompare | times: item.quantity %}

{{ item.variant.options[forloop.index0] }} {{ item.product.price[forloop.index0] }}

{% if item.product.compare_at_price > item.product.price %} {{ beforePrice | money }} {% endif %}

如果您只希望它在循环的第一次迭代中输出一次并假设您正在循环变体(这可能有助于查看所有循环),您可以使用 forloop.first


{% if forloop.first %}
  {{ item.variant.options[forloop.index0] }} {{ item.product.price[forloop.index0] }}
{% endif %}

或者您可能会发现您可以省去循环并执行

{{ item.variant.options.first }} {{ item.product.price.first }}