显示集合中购物车Shopify中不存在的项目

显示集合中购物车Shopify中不存在的项目,shopify,liquid,Shopify,Liquid,我试图迭代购物车中的项目,并生成一个不在购物车中的产品的追加销售div。下面是我到目前为止的代码,但我在向购物车添加两个项目时遇到了问题,两次运行循环,两次生成html。有什么解决办法吗?我被难住了 {% for item in cart.items %} // iterates over items in cart {% if item.product.id == 4456879040188 %} // checks if product id matches in item in ca

我试图迭代购物车中的项目,并生成一个不在购物车中的产品的追加销售div。下面是我到目前为止的代码,但我在向购物车添加两个项目时遇到了问题,两次运行循环,两次生成html。有什么解决办法吗?我被难住了

{% for item in cart.items %} // iterates over items in cart

  {% if item.product.id == 4456879040188 %} // checks if product id matches in item in cart
    <div class="upsell-pop" style="text-align:center; width: 100%;">
      <h4>Frequently bought together</h4>
      {% for prod in collections.upsell.products %} // iterates products in collection upsell
        {% unless prod.handle contains "product-name" %} // shows only prods that do not contain url handle
            <div>
              <span class="upsell-title">{{ prod.title }}</span>
              <span class="upsell-price">{{ prod.metafields["meta"]["promo"] }} {{ prod.price | money }}</span>
              <a href="{{prod.url}}"><img src="{{ prod.featured_image | img_url: '200x' }}" /></a>
              <a class="btn-product" href="{{prod.url}}">View Product</a>
            </div>
        {% endunless %}
      {% endfor %}
    </div>
  {% endif %}

{% endfor %}
{%for cart.items%}//迭代购物车中的项目
{%if item.product.id==4456879040188%}//检查购物车中的商品id是否匹配
经常一起买
{collections.upsell.products%}//迭代集合upsell中的产品
{%除非prod.handle包含“product name”%}//否则只显示不包含url句柄的产品
{{prod.title}
{{prod.metafields[“meta”][“promo”]}{{prod.price | money}}
{%end除非%}
{%endfor%}
{%endif%}
{%endfor%}
另一个想法是以某种方式检查产品是否不在购物车项目中,以替换现有的“除非”语句,但不确定如何对其进行编码

{% unless cart.items exist then %} // I know this is not correct syntax
     <div>
          <span class="upsell-title">{{ prod.title }}</span>
          <span class="upsell-price">{{ prod.metafields["meta"]["promo"] }} {{ prod.price | money }}</span>
          <a href="{{prod.url}}"><img src="{{ prod.featured_image | img_url: '200x' }}" /></a>
          <a class="btn-product" href="{{prod.url}}">View Product</a>
      </div>
{% endunless %}
{%除非存在cart.items,否则%}//我知道这是不正确的语法
{{prod.title}
{{prod.metafields[“meta”][“promo”]}{{prod.price | money}}
{%end除非%}

在我看来,这里有两个步骤

首先,捕获您的购物车内容,以便在循环使用upsell集合时获取要比较的字符串。这可能是这样的:

{%- capture cart_items -%}
  {% for item in cart.items %}
    {{ item.product.handle }}{% unless forloop.last %} , {% endunless %}
  {% endfor %}
{%- endcapture -%}
然后在每次迭代中检查字符串是否不包含当前产品的句柄时,循环遍历集合:

{% for product in collections['upsell'].products %}
  {% unless cart_items contains product.handle %}
    {{ product.title }}
  {% endunless %}
{% endfor %}
备注:

Snippet 1=>在line_item(此处称为item,因为它写起来比较短)对象中,您可以访问产品对象属性:item.product.product_attr_needed

Snippet 2=>要使用集合对象的句柄直接访问集合对象,必须使用集合+包含集合句柄+属性的方括号。这里的“upsell”是upsell集合的句柄

没有测试,但这应该是可行的


HTH

没有人能回答这个问题:/