代码之家  ›  专栏  ›  技术社区  ›  mwalker

在Shopify中检查购物车

  •  1
  • mwalker  · 技术社区  · 8 年前

    我希望在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 %}
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Radhesh Vayeda    8 年前

    我认为这些步骤可能会帮助你。。。

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

    第2步: 在购物车中遵循以下代码。液体

    {% 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 %}
    
        2
  •  1
  •   Victor Leontyev    8 年前

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

    {% 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 %}