Adding two metafields together through math

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.

Product.metafields.my_fields.turn + product.metafields.my_fields.fade = product.custom.stabilitet

I.e the value in the fields would be (-3) + 1 = custom.stabilitet/-2

The action is update product metafield

I have tried serveral differnt soultions but cant find any that works.

My latest try is:

{% assign metafield1 = product.metafields.my_fields.turn.value %}

{% assign metafield1 = product.metafields.my_fields.fade.value %}

{% assign custom.stabilitet = metafield1 | plus: metafield2 %}

Have also tried to write

product.metafields.my_fields.turn + product.metafields.my_fields.fade

All the three fields are setup as content type: decimal.

Someone know how to write the code?

Hi Viktor,

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:

{% assign metafield1 = product.metafields.my_fields.turn.value | plus: 0 %}

{% assign metafield2 = product.metafields.my_fields.fade.value | plus: 0 %}

{% assign custom.stabilitet = metafield1 | plus: metafield2 %}
Also note both your variables in the code you provided are metafield1.
Cheers,

Hi Viktor_Torner,

Two things here should help you out:

  1. Like Paquita said, adding the | plus: 0 filter will essentially cast the string values to numbers that can be used in math later.
  2. 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:
{% assign turn_metafield = product.metafields | where: "namespace", "my_fields" | where: "key", "turn" | first %}
{% assign fade_metafield = product.metafields | where: "namespace", "my_fields" | where: "key", "fade" | first %}

{% assign turn_value = turn_metafield.value | plus: 0 %}
{% assign fade_value = fade_metafield.value | plus: 0 %}

{% assign stabilitet = turn_value | plus: fade_value %}

More information on Liquid and Metafields in Flow can be found in Flow’s Variables help docs

Hope that helps!

Thanks a lot!
I got an error message.
Got error updating metafield: “Value must be a decimal.” For value: “\n\n\n\n\n\n”

THe field is setup as a decimal fields. Could it be that causing the error? Should it be something else, like JSON?

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:

{%- assign turn_metafield = product.metafields | where: "namespace", "my_fields" | where: "key", "turn" | first -%}
{%- assign fade_metafield = product.metafields | where: "namespace", "my_fields" | where: "key", "fade" | first -%}
{%- assign turn_value = turn_metafield.value | plus: 0 -%}
{%- assign fade_value = fade_metafield.value | plus: 0 -%}
{%- assign stabilitet = turn_value | plus: fade_value -%}
{{- stabilitet -}}

Thanks a lot, to all of you! This one worked well!
Learned something new as well with thehyphens. :+1:

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?

{%- assign stabilitet = product.metafields | where: “namespace”, “custom” | where: “key”, “stabilitet” | first -%}
{%- assign stabilitet_value = stabilitet | plus: 0 -%}
{% if stabilitet_value > 0.0 %}
{%- assign stabilitet2 = “Överstabil” -%}
{% else %}
{%- assign stabilitet2 = “Understabil” -%}
{% endif %}
{{- stabilitet2 -}}

Hi Viktor,
This line:
{%- assign stabilitet_value = stabilitet | plus: 0 -%}
I believe it should be:
{%- assign stabilitet_value = stabilitet**.value** | plus: 0 -%}

It came out as previous. Always choosing the Understabil.

Hi Viktor,
have you resolved this?
I am currently calling my customer metafields and using this line of code:

{% assign counters = customer.metafields.customerx.session_counter.value %}
you could try:
{%- assign stabilitet = product.metafields.custom.stabilitet.value -%}

{%- assign stabilitet_value = stabilitet | plus: 0 -%}

or if product.metafields.custom.stabilitet.value is an array:
{%- assign stabilitet_0_value = stabilitet[0] | plus: 0 -%}

{%- assign stabilitet_1_value = stabilitet[1] | plus: 0 -%}
… etc.

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

Try adding the hyphen to each of the {% %} blocks above. It looks like only some of them have it but they all should.

Also avoid other whitespace outside of the Liquid (like if there’s a leading new line or anything)

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.

Instead of:

{%- assign stabilitet_value = stabilitet | plus: 0 -%}

Try:

{%- assign stabilitet_value = stabilitet.value | plus: 0 -%}

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?

{%- assign stabilitet = product.metafields | where: “namespace”, “custom” | where: “key”, “stabilitet” | first -%}
{%- assign stabilitet_value = stabilitet.value | plus: 0 -%}
{%- if stabilitet_value < 1 -%}
{%- assign stabilitet1 = “Overst” -%}
{% else %}
{%- assign stabilitet1 = “Under” -%}
{% endif %}
{{-stabilitet1-}}

Have you confirmed what that value is by sending it to an email or something like that?

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.

The value is in the field.

{%- assign stabilitet_metafield = product.metafields | where: “namespace”, “custom” | where: “key”, “stabilitet” | first -%}
{%- assign stabilitet_value = stabilitet_metafield.value | plus: 0 -%}