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 %}
​
Solved! Go to the solution
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 %}
​
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.
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.
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
Rather Ruby: https://github.com/Shopify/liquid
User | Count |
---|---|
38 | |
24 | |
16 | |
12 | |
10 |