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

Solved

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

MGNetworks
Excursionist
21 1 11

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

Accepted Solution (1)

MGNetworks
Excursionist
21 1 11

This is an accepted solution.

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

View solution in original post

Replies 2 (2)

MGNetworks
Excursionist
21 1 11

This is an accepted solution.

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

Shreyaaa
Visitor
3 0 0

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 ?