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 %}
pvoulg
March 5, 2021, 10:49pm
2
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 %}
1 Like
thanks for the reply. unfortunately this hasn’t worked.
i added some debug.
{% assign inventory = product.metafields.evey.event.inventory | plus: 0 %}
{{ inventory }} inventory
{% assign attendee_count = product.metafields.evey.event.attendee_count | plus: 0 %}
{{ attendee_count }} attendee count
{% assign total_tickets = inventory | plus: attendee_count %}
{{ total_tickets }} total tickets
{% assign percent_sold = attendee_count | divided_by: total_tickets | times: 100 %}
{{ percent_sold }} percent sold
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.
pvoulg
March 5, 2021, 11:13pm
4
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.
tim_1
March 6, 2021, 2:31am
5
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
2 Likes
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?
tim_1
March 7, 2021, 1:22pm
7