在 Shopify 中检查购物车

Examine cart in Shopify

我希望在 Shopify 中获得有关 liquid 的帮助,我在尝试根据客户添加的产品自定义购物车时遇到了问题。

基本上,如果客户添加供应商 A 的产品,那么我希望购物车加载模板以自定义供应商 A 的购物车

但是如果产品来自供应商 B,那么我希望它加载模板来为供应商 B 定制购物车

但是如果购物车中没有任何一个(或两个)的产品,那么我希望它加载默认购物车。

{编辑:我弄清楚了加载模板的代码有什么问题,但现在我只需要逻辑方面的帮助,以便当购物车包含两个品牌的产品时,它会加载默认购物车。因为目前它会将两个购物车片段加载到页面中}

非常感谢任何帮助!

{% for item in cart.items %}
{% if item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% elsif item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% else %}

{% section 'cart-default %}

{% endif %}
{% endfor %}

也试过这个:

{% case cart.items %}
{% when item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% when item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% when item.vendor == ‘Brand A’ and item.vendor == 'Brand B' %}
{% section 'cart-default' %}
{% else %}
{% section 'cart-default' %}
{% endcase %}

在液体中使用阵列更容易。适合您的工作代码:

{% assign vendors = cart.items | map: 'vendor'| uniq | join: ' ' %}
{% if  vendors contains "Brand A" and vendors contains "Brand B" %}
    {% section 'cart-default' %}
{% else %}
    {% if  vendors contains "Brand A" %}
        {% section 'cart-a' %}
    {% else %}
        {% if  vendors contains "Brand B" %}
            {% section 'cart-b' %}
        {% else %}  
            {% section 'cart-default' %}
        {% endif %}
    {% endif %}
{% endif %}

我认为这些步骤可能对您有所帮助...

第 1 步:为两种不同类型的供应商和默认创建不同的部分而不是片段

第 2 步: 按照以下代码在 cart.liquid

{% assign vendor = '' %}
{% assign same = true %}
{% for item in cart.items %}
    {% if vendor != '' or vendor == item.vendor %}
         {% assign vendor = item.vendor %}
    {% else%}
         {% assign same = false %}
    {% endif %}
{% endfor %}

{% if same == true %}
    {% if vendor == 'Brand A' %}
       {% section 'cart-a' %}
    {% elsif vendor == 'Brand B'%}
       {% section 'cart-b' %}
    {% else %}
       {% section 'cart-default' %}
    {% endif %}
{% else %}
    {% section 'cart-default' %}
{% endif %}