How can I correctly calculate total savings on multiple items in a cart?

Topic summary

A developer encountered an issue calculating total savings across multiple cart items in what appears to be a Shopify Liquid template.

The problem: When adding multiple discounted items (e.g., two shoes at 50% off), the savings calculation only reflected one item instead of the total.

The solution: The original code used item.item_count which doesn’t account for quantity. Changing it to item.quantity fixed the calculation, properly multiplying the per-item savings by the quantity of each product.

Code change:

  • Before: item.item_count :times
  • After: item.quantity :times

A follow-up question asks where to implement this code and how to display a “Save $10” badge on product cards alongside the cart functionality, but remains unanswered.

Summarized with AI on November 12. AI used: claude-sonnet-4-5-20250929.

Hey there, i’m trying to calculate the saved amount on the total cart items.
It works quite well, but let’s say i buy a shoe worth 20$ with 50% off, so that would be 10$.
Now it would say i saved 10$, but when i add a second shoe it does not say i saved 20$, it will still say 10$.

How to fix that?
The part of code is below:

{% assign total_saving = 0 %}
{% for item in cart.items %}
{% if item.variant.compare_at_price > item.variant.price %}
{% capture saving %}{{ item.variant.compare_at_price | minus: item.variant.price | times: item.item_count}}{% endcapture %}
{% assign total_saving = saving | plus: total_saving %}
{% endif %}
{% endfor %}
{{ total_saving | money }} EUR

Fixed it myself right now..

For everyone having the same issue: The new code is below

{% assign total_saving = 0 %}
{% for item in cart.items %}
{% if item.variant.compare_at_price > item.variant.price %}
{% capture saving %}{{ item.variant.compare_at_price | minus: item.variant.price | times: item.quantity}}{% endcapture %}
{% assign total_saving = saving | plus: total_saving %}
{% endif %}
{% endfor %}
{{ total_saving | money }} EUR

(Only thing that changes is “times: item.item_count” → “times: item.quantity”) lol, easy fix :Pd

Hello , That’s a really great idea , but will you please let me know where to add this code ??? Please?? And second thing I want to shown on product card also along with cart " Save 10$ " badge then how can we do that ?