How do you add 2 shopify variables together?

Solved

How do you add 2 shopify variables together?

michaeluchiha
Shopify Partner
6 0 2

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 }}



Accepted Solution (1)

StoreWatchers
Trailblazer
205 30 36

This is an accepted solution.

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 %}
<h1>Yearly: {{ yearly | money }}</h1>
<h1>Monthly: {{ monthly | money }}</h1>
{% assign final_price = yearly | plus: monthly %}
<h1>Final Price: {{ final_price | money }}</h1>

 

 

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

StoreWatchers_2-1671613149647.jpeg

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

Let me know if you need further assistance.  

StoreWatchers - Automate testing for your Shopify store in seconds


If you find my reply helpful, please hit Like and Mark as Solution
Visit us: App Store | Website | FAQs

View solution in original post

Replies 3 (3)

StoreWatchers
Trailblazer
205 30 36

This is an accepted solution.

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 %}
<h1>Yearly: {{ yearly | money }}</h1>
<h1>Monthly: {{ monthly | money }}</h1>
{% assign final_price = yearly | plus: monthly %}
<h1>Final Price: {{ final_price | money }}</h1>

 

 

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

StoreWatchers_2-1671613149647.jpeg

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

Let me know if you need further assistance.  

StoreWatchers - Automate testing for your Shopify store in seconds


If you find my reply helpful, please hit Like and Mark as Solution
Visit us: App Store | Website | FAQs
michaeluchiha
Shopify Partner
6 0 2

Legend.

StoreWatchers
Trailblazer
205 30 36

Thank you 🙂

Glad to know it was helpful! 

StoreWatchers - Automate testing for your Shopify store in seconds


If you find my reply helpful, please hit Like and Mark as Solution
Visit us: App Store | Website | FAQs