Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Here's my code:
{% for line_item in line_items | sort: "line_item.product.type" %}
<tr>
<td>{{ line_item.quantity }} x</td>
<td><b>{{ line_item.title }}</b></td>
{% if show_line_item_taxes %}
<td>
{% for tax_line in line_item.tax_lines %}
{{ tax_line.price | money }} {{ tax_line.title }}<br/>
{% endfor %}
</td>
{% endif %}
<td>
{% if line_item.original_price != line_item.price %}
<s>{{ line_item.original_price | money }}</s>
{% endif %}
{{ line_item.price | money }}
</td>
<td>{{ line_item.product.type }}</td>
</tr>
{% endfor %}
I need to sort by product type (Category). Thank you.
Solved! Go to the solution
This is an accepted solution.
Hi there,
You can try something along these lines, it should accomplish what you want. It's not pretty but not ugly either, I'm also curious to know if there's a way to filter/sort nested properties more efficiently, but Im' not aware of it :
{% assign line_items = cart.items %}
{% assign types = line_items | map: 'product' | map: 'type' | sort_natural | uniq %}
{% for type in types %}
{% for line_item in line_items %}
{% if line_item.product.type == type %}
your code...
{% endif %}
{% endfor %}
{% endfor %}
So basically, I take the problem differently. I first make a list of all product types in the cart, sort them alphabetically, and remove the duplicate types. We loop through each type, and then loop through each line_item inside of that, which allow us to filter out line_items types that are not matching the type from the main loop.
Cheers
This is an accepted solution.
Hi there,
You can try something along these lines, it should accomplish what you want. It's not pretty but not ugly either, I'm also curious to know if there's a way to filter/sort nested properties more efficiently, but Im' not aware of it :
{% assign line_items = cart.items %}
{% assign types = line_items | map: 'product' | map: 'type' | sort_natural | uniq %}
{% for type in types %}
{% for line_item in line_items %}
{% if line_item.product.type == type %}
your code...
{% endif %}
{% endfor %}
{% endfor %}
So basically, I take the problem differently. I first make a list of all product types in the cart, sort them alphabetically, and remove the duplicate types. We loop through each type, and then loop through each line_item inside of that, which allow us to filter out line_items types that are not matching the type from the main loop.
Cheers
It works, thank you very much!
I am trying to implement your solution, with no luck. I am using this code below to sort the packing slip alphabetically, but I want to further "group" by Product Type, and then sort alphabetically within each "group" of Product Type.
{% assign lineitems = line_items_in_shipment | sort: "title" %}
{% for line_item in lineitems %}
How would I integrate this with your solution? I tried to put my code in your code where it says "your code", but it returns a syntax error.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024