Shopify Sort cart.items array using Liquid script

二次信任 提交于 2019-12-23 01:40:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!