1

I want to sort the cart.items array in the cart.liquid file. And then I can display the times in the table in my expected order.

    {% for item in cart.items sort_by:item.line_price %}
    <div class="row">
      <div class="span12">
      <h1>Your cart</h1>
      <form action="/cart" method="post" id="cartform">
        <table>
        </table>
      </form>
      </div>
    </div>
    {% endfor %}

But the code doesn't work since I can't use sort_by:item.line_price in for statement.

How can I sort an array using built-in features?

The other problem is Shopify liquid doesn't support to create an array. if I use my own algorithm to sort the array. But how can I save the output to a new array?

Thanks a lot.

4

2 回答 2

6

Shopify has a sort filter for "an array of hashes or drops". You could use it this way:

{% assign cart_items = cart.items | sort: "line_price" %}
{% for item in cart_items %}
- {{item.title}} {{item.line_price}}<br/>
{% endfor %}
于 2013-09-03T14:20:12.670 回答
2

您可以将数据呈现为 Javascript 数据结构,然后对其进行排序。在 Javascript 中使用模板。Handlebars.js 可以很好地解决这个问题。没有内置排序。

于 2013-08-23T03:25:39.743 回答