问题
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.
回答1:
You could just render your data to a Javascript data structure and then sort it. Use a template in Javascript. Handlebars.js works well for that. There is no built-in sort.
回答2:
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 %}
来源:https://stackoverflow.com/questions/18392766/shopify-sort-cart-items-array-using-liquid-script