Skip to content

Instantly share code, notes, and snippets.

@thecatmelon
Created August 14, 2015 18:07
Show Gist options
  • Save thecatmelon/e6779626245f91e5286a to your computer and use it in GitHub Desktop.
Save thecatmelon/e6779626245f91e5286a to your computer and use it in GitHub Desktop.
Calculate the total weight of the order for the order printer template

The weight_with_unit filter wasn't working for me so I did some cool filtery math stuff to get it to work.

Total weight : {{ total_weight | times: 2.20462262 | divided_by: 1000 | round: 1 }} {% if total_weight == 1 %}lb{% else %}lbs{% endif %}

Order: Total weight

Loops through all the line items to calculate the total weight:

{% assign total_weight = 0 %}
{% for line in line_items %}
  {% assign total_weight = line.quantity | times: line.grams | plus: total_weight %}
{% endfor %}
Total weight : {{ total_weight | weight_with_unit }}

Another add on to get the total number of items. Add this line into the for loop above, then output it

 {% assign total_items = line.quantity | plus: total_items %}
 Total Number of Pieces : {{ total_items }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment