HI!
Im trying to add/sum two values of metafields together.
The trigger is product created/update then I want it to update a third metafields with the sum of the two previous.
Not sure if it will help, but I have found that you need to make sure that the code treats your variables a numbers and not strings. To do that you need to add a number at the point of assigning the variables. Try this:
Like Paquita said, adding the | plus: 0 filter will essentially cast the string values to numbers that can be used in math later.
Metafields in Flow are not accessible through the dot-delimited syntax like in Online Store, so you need to use where and first filters to find the metafields. Here is a quick example that should help you out:
The liquid is inserting line breaks. It’s also looks like your aren’t actually outputting the calculated value. To remove line breaks you can either put everything into one line or use hyphens in the tags. To output you need something like {{ stabilitiet }}. Putting that together:
Hi!
This one should maybe be posted in another line but trying here since it is connected to the previous question.
I have tried out from the previous answer to make another metafield. The code looks like this but its always chooses Understabil. I believe it doesnt get the value in stabilitet_value right, or that the if statement doesnt work properly with the metafields. Do you know why its always chooses the second option and what change in the code it need to be?
Hi!
Nope have not gotten it to work. Now it tells me this:
Got error updating metafield: “Value must be a single line text string.” For value: “\nUnder”
I believe now I dont get it to understand its a single text line.
THis is how the code looks like at the moment:
{%- assign stabilitet = product.metafields | where: “namespace”, “custom” | where: “key”, “stabilitet” | first -%}
{%- assign stabilitet_value = stabilitet | plus: 0 -%}
{%- if stabilitet_value > 1 -%}
{%- assign stabilitet1 = “Overst” -%}
{% else %}
{% assign stabilitet1 = “Under” %}
{% endif %}
{{ stabilitet1 }}
My bad, have changed the code several times.
This one, is according to your writing and now the text comes out.
The value in the stabilitets field can be 3.0 or -3.0, it always chooses Under.
-3.0 is correct but 3.0 should be Overst, but Under comes out in the field.
Nope.
But I did switch the > to < and then Overst comes out for the first time. But Under doesnt comes out at all. So I would believe it has problem to understand that 1 is a value somehow?
Yepp ,that was the problem. The value is zero in the mail.
Dont get it why it doesnt “catch” the value from the field since Im using the same code as earlier, but changed the namespace and key to what it should be.