can you create and then use a variable in liquid?

Solved
bytejunkie
Tourist
5 0 1

the following code doesn't work, but i believe it should. it hinges about the ability to create a variable in one step, then use it in the next. i think it should work, as i've assigned and then reassigned a variable in a loop. 

so is there something wrong with the steps below?

 

{% assign total_tickets = product.metafields.evey.event.inventory | plus: product.metafields.evey.event.attendee_count %}
    	{% assign percent_sold = product.metafields.evey.event.attendee_count | divided_by: total_tickets | times: 100 %}
    	​

 

   
0 Likes
pvoulg
Shopify Partner
173 26 49

Hi @bytejunkie

Since you're using math filters, make sure the variables you're creating are all viewed as numbers and not as strings. You can convert a string into a number in liquid, by adding 0, or multiplying by one. Just to make sure this works, I'd do the following:

{% assign inventory = product.metafields.evey.event.inventory | plus: 0 %}
{% assign attendee_count  = product.metafields.evey.event.attendee_count  | plus: 0 %}

{% assign total_tickets = inventory  | plus: attendee_count  %}
{% assign percent_sold = attendee_count  | divided_by: total_tickets | times: 100 %}
    	​

 

Panos Voulgaris
Creative director / Partner @ MALVI
Founder @ function( )
bytejunkie
Tourist
5 0 1

thanks for the reply. unfortunately this hasn't worked.

i added some debug.

      	{% assign inventory = product.metafields.evey.event.inventory | plus: 0 %}
      <p>{{ inventory }} inventory</p>
		{% assign attendee_count  = product.metafields.evey.event.attendee_count  | plus: 0 %}
      <p>{{ attendee_count }} attendee count</p>
		{% assign total_tickets = inventory | plus: attendee_count   %}
      <p>{{ total_tickets }} total tickets</p>
      {% assign percent_sold = attendee_count | divided_by: total_tickets | times: 100 %}
      <p>{{ percent_sold }} percent sold</p>

 

to get this

2348 inventory

1006 attendee count

3354 total tickets

0 percent sold

 

that percentage sold sum still isnt working.  im lost as to why, given the others should be forced to numbers as per your additions.

0 Likes
pvoulg
Shopify Partner
173 26 49

Just to further debug, try breaking down the last operation in two parts, division in one step, multiplication in the next. Sometimes calculating with variables can be a bit wonky.

Panos Voulgaris
Creative director / Partner @ MALVI
Founder @ function( )
0 Likes
tim
Shopify Expert
2926 143 1021

This is an accepted solution.

That's because your numbers are integers.

Basically, {{ 3 | divided_by: 5 | times: 100 }}  or even {{ 3 | divided_by: 5 }}  will produce the same result -- 0.

You need to make'em floats, like this: {{ 3 | times: 100.0 | divided_by: 5 }} This will produce the result you want.

In your case:

 

{{ attendee_count | times: 100.0 | divided_by: total_tickets }}
  29.994036970781156
{{ attendee_count | times: 100   | divided_by: total_tickets }}
  29
{{ attendee_count | times: 100.0 | divided_by: total_tickets | round: 2 }}
 29.99

 

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
bytejunkie
Tourist
5 0 1

appreciate replys from both of you, but the conversion to floats was the issue. 

does that imply that under the hood the filters run under js?

0 Likes
tim
Shopify Expert
2926 143 1021

Rather  Ruby: https://github.com/Shopify/liquid 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
0 Likes