How do you add 2 shopify variables together?

Hi I have the following code which aims to get the final price of 2 variant items and add them together.

{% for item in cart.items %}
{% if item.id == 44026338640147 %}
{% capture yearly %}
{{ item.final_line_price | times: 3 | money }}
{% endcapture %}
{% elsif item == 44026338607379 %}
{% capture monthly %}
{{ item.final_line_price | times: 36 | money }}
{% endcapture %}
{% endif %}
{% endfor %}

I want to try to use this method to add these values together like i do in JS but it doesnt let me:
{% final price = yearly + monthly %}
{{ monthly }}

Hello @michaeluchiha ,

To achieve the result you looking for you should use the below code :

{%- for item in cart.items -%}
 {% if item.id == 42042400604416 %}
  {% capture yearly %}
  {{ item.final_line_price | times: 3 }}
  {% endcapture %}
 {% elsif item.id == 42814905024768 %}
  {% capture monthly %}
  {{ item.final_line_price | times: 36 }}
  {% endcapture %}
 {% endif %}
{% endfor %}
# Yearly: {{ yearly | money }}
# Monthly: {{ monthly | money }}
{% assign final_price = yearly | plus: monthly %}
# Final Price: {{ final_price | money }}

I have tested it on my end and it is working well. Here is the output :

Note: Please update the item.id into your product’s actual item id.

Let me know if you need further assistance.

1 Like

Legend.

1 Like

Thank you :slightly_smiling_face:

Glad to know it was helpful!