Sort Order Printer by product.type then line_item.title

Shopify Partner
55 0 0

How do we sort our Order Printer line items first by product.type then by line_item.title. Currently we are able to sort by the line item title as below. How do we first sort by product.type.

{% assign line_items = line_items | sort: 'title' %}

Thank you,

Chuck Burr
Restoration Seeds

Chuck Burr Restoration Seeds
0 Likes

Hi Chuck,

I've played around with this in the past on our Order Printer Templates, and unfortunately never found a way to sort something by a field not on the object itself. So you can sort by anything on the line_item object itself, but as the type is on the product object, then you cannot do it :(

Maybe someone else has found a solution, but it'll take some pretty complex liquid to do.

Cheers,
Bjorn 

Bjorn Forsberg | FORSBERG+two | Award-winning Shopify Apps since 2011
0 Likes
Shopify Partner
55 0 0

It should be easy in liquid but its not as you said. It's like having an apparel company and not being able to sort by type: shirt, pants, sox. I am disapointed that no one from Shopify replied to this post either or would help from the Support department. Here is the solution in JS:

<div style="font-family=Lucida Grande, Arial; margin-left: 40px; margin-right: 40px";>
<img src="https://cdn.shopify.com/s/files/1/0129/8922/files/RS_Two_Lines_Black_130.png?94311"; alt="{{ shop_name}}" height="88px" />
<p style="font-size:9pt; float: right; text-align: right;">
  {{ "now" | date: "%m/%d/%y" }}<br /><br />
  {{ shop_name }}<br />
  {{ shop.address }}<br/>
  {{ shop.city }}, {{ shop.province_code }} {{ shop.zip | upcase }}<br/>
  United States
</p>

<h4>Order {{ order_name }}</h4>

 

<div style="font-size:10pt; padding: 8px; border: 1px solid black;">
{% assign line_items = line_items %}
<div class="order-items-{{ order_name | replace: '#', ''  }}">
{% for line_item in line_items %}
<li style="margin-left:0px; list-style-type:none;" data-product-type="{{ line_item.product.type }}-{{ line_item.product.title }}">{{ line_item.quantity }} x {{ line_item.product.type }} {{ line_item.product.title }} - {{ line_item.variant.title }} &mdash; {{ line_item.price | money }}</li>
{% endfor %}
</div>
</div>
<script>

// Sort items by TYPE using JS magic ;)
console.log(jQuery(".order-items-{{ order_name | replace: '#', ''  }} li"));
jQuery(".order-items-{{ order_name | replace: '#', ''  }} li").sort(sort_li).appendTo('.order-items-{{ order_name | replace: '#', ''  }}');
function sort_li(a, b){
console.log(a);
    return (jQuery(b).data('product-type')) < (jQuery(a).data('product-type')) ? 1 : -1;    
}

// 

</script>

<h4>Amount Paid</h4>

<div style="font-size:10pt; padding: 8px; border: 1px solid black;">
Subtotal: {{ subtotal_price | money }}<br/>
{% for discount in discounts %} Includes Discount: {{ discount.code }} {{ discount.savings | money }}<br/>{% endfor %}
Tax: {{ tax_price | money }}<br/>
{% if shipping_address %} Shipping: {{ shipping_price | money }} {% endif %}<br/>
<strong>Payment: {{ total_price | money }}</strong><br/>
{% if total_paid != total_price %} <strong>Refunded: {{ total_price | minus: total_paid | money }}</strong><br/>
<strong>Net Payment: {{ total_paid | money }}</strong><br/>
{% endif %}
</div>

{% if shipping_address %}
  <h4>Shipping Address</h4>
<div style="font-size: 9pt; padding: 8px; border: 1px solid black; text-transform:capitalize;">
  {{ shipping_address.>name }}<br/>
  {% if shipping_address.company %}
    {{ shipping_address.company }}<br/>
  {% endif %}
  {{ shipping_address.street }}<br/>
  {{ shipping_address.city }}
  {{ shipping_address.province_code }}
  {{ shipping_address.zip | upcase }}<br/>
  {% if shop.country != if shipping_address.country_code %}
    {{ shipping_address.country }}
  {% endif %}
</div>
{% endif %}

{% if note %}
  <h4>Note</h4>
  <div style="font-size: 9pt; padding: 8px; border: 1px solid black; text-transform:capitalize;">
  {{ note }}
  </div>
{% endif %}

<div style="font-size:9pt; text-align: center; "><p>www.restorationseeds.com | orders@restorationseeds.com | Thank you for supporting family farms and OP seeds.</p></div>
</div>

Chuck Burr Restoration Seeds
0 Likes

Thanks Chuck, nice solution :)

Bjorn Forsberg | FORSBERG+two | Award-winning Shopify Apps since 2011
0 Likes
New Member
1 0 0

Hi Chuck_Burr,

I need to sort order items, too, and found your post. Tried to use your code but I get an error on save "Body contains unsupported tags or attributes: script".
I guess this check was introduced in the meantime.

Anybody else has a solution?

Thanks

UPDATE
A really bad but quick solution (if you've got just a few product Types): iterate throught line_items as many times as Types you have and each time print just one Type. I use this in my picking list to order items as they are in the shelves in my warehouse.


IE: you have two Types - fruit, sweets - and want to show fruits first and then sweets.

{% for line_item in line_items %}
{% if line_item.product.type == 'fruit' %} 

<tr>
<td><b>{{ line_item.title }}</b></td>
<td>{{ line_item.quantity }}</td>
</tr>

{% endif %}
{% endfor %}

{% for line_item in line_items %} {% if line_item.product.type == 'sweets' %} <tr> <td><b>{{ line_item.title }}</b></td> <td>{{ line_item.quantity }}</td> </tr> {% endif %} {% endfor %}

Pay attention not to miss any list_item! This code shows just product of the two Types checked, all the rest will be ignored.



0 Likes