【问题标题】:How to append tags in a forlop to an array in shopify liquid如何将forlop中的标签附加到shopify液体中的数组
【发布时间】:2021-08-04 17:43:31
【问题描述】:

我正在尝试创建一个已经有一些标签的数组。我想遍历购物车中的每个产品并将标签添加到数组中。 {{tag }} 部分正在工作,但它没有被分配给数组..

{% assign finalTaglist = "apples, oranges, peaches" | split: ", " %}
        
        {% for item in cart.items %}
          <p>
            {% for tag in item.product.tags %}
                {{tag}}
            
                {% assign finalTaglist = finalTaglist | concat: tag %}
            
            {% endfor%}
          </p>
        {% endfor%}
        
        <p>Final Tag List : {{finalTaglist}}</p>`

`

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    concat 用于连接数组,但您的代码试图将字符串添加到数组中,因此它不起作用。

    从一个空字符串开始,然后使用append 添加到字符串中,不要忘记您的分隔符。构建完成后,使用split 创建数组,如果需要,您可以append 到另一个数组。

    类似的东西(未经测试,只是暂时的,但你明白了..)

    {% assign finalTaglist = 'apples,oranges,peaches' | split: ',' %}
    
    {% assign newTagList = '' %}
    {% for item in cart.items %}
        {% for tag in item.product.tags %}
            {% assign newTagList = newTagList | append: ',' | append: tag %}
        {% endfor%}
    {% endfor%}
    
    {% assign newTagList = newTagList | remove_first: ',' | split: ',' %}
    
    {% assign joinedTagLists = finalTaglist | concat: newTagList %}
    
    猜你喜欢
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2016-12-23
    相关资源
    最近更新 更多