Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Shopify Flow: Value from custom Customer Metafield is returned with multiple newlines

Shopify Flow: Value from custom Customer Metafield is returned with multiple newlines

sahand
Shopify Partner
5 0 1

Hi everyone,

 

Been dealing with a big problem with updating a custom order metafield and mapping it with a value from a custom customer metafield in Shopify Flow:

 

Here is a snapshot of the flow:

Screenshot 2023-05-31 at 11.02.00 AM.png

 

Basically in my code after an Order is created it will do an Update Order Metafield action:

Screenshot 2023-05-31 at 10.51.29 AM.png

 

Here is the code for the value: 

 

{% for getCustomerData_item in getCustomerData %}
{% for metafields_item in getCustomerData_item.metafields %}

{% if metafields_item.namespace == "customer" and metafields_item.key == "s4hc_billto" %}

{{metafields_item.value}}

{% endif %}

{% endfor %}
{% endfor %}

 

I have looked online and this code to me looks correct, however, two things are occuring:

Screenshot 2023-05-31 at 10.55.23 AM.png

1. The value is not an integer... I don't know if this will be fixed if the value is returned correctly but mainly

2. The value is returned as "\n \n\n \n\n \n\n \n\n\n\n \n \n 12345678\n\n \n\n\n\n"

 

Why is the value doing that. The value in the customer metafield is just 12345678 I have no clue why this is occurring. I've tried to use other liquid functions such as strip or even remove:  {{metafields_item.value | remove: '\n' | remove: ' ' }} but to no avail...

 

Please if anyone can help me figure this out with any tips of where to check it would be much appreciated

Replies 3 (3)

paul_n
Shopify Staff
1433 157 332

So first, I'm not sure why you are using Get Customer Data. You can get the customer on the order just using:

{{ order.customer.fieldName }}

Liquid has the unfortunate side-effect of inserting line breaks for every line and the metafield action is very strict. So you need to remove the whitespace causing those newlines "\n" and spaces. 

 

To do that you use hyphens in your tags,

{%- for getCustomerData_item in getCustomerData -%}
{%- for metafields_item in getCustomerData_item.metafields -%}
{%- if metafields_item.namespace == "customer" and metafields_item.key == "s4hc_billto" -%}
{{- metafields_item.value -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

Digico
Shopify Partner
47 1 5

As @paul_n said, Liquid code create a lot of whitespace and empty lines. Putting - inside Liquid code trim this and solve the issue. Use the suggested code from him, it should work and clear all "/n /n" inside your code, thus making your flow work. Quoting Paul_n:

 

 

{%- for getCustomerData_item in getCustomerData -%}
{%- for metafields_item in getCustomerData_item.metafields -%}
{%- if metafields_item.namespace == "customer" and metafields_item.key == "s4hc_billto" -%}
{{- metafields_item.value -}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}

 

 

How does it work now?

ChunkySteveo
Shopify Partner
21 0 6

Try removing the whitespace with the {%-  -%} code blocks? i.e. putting a hyphen before and after strips out any white space in the Liquid code.

 

{%- for getCustomerData_item in getCustomerData -%}
{%- for metafields_item in getCustomerData_item.metafields -%}
{%- if metafields_item.namespace == "customer" and metafields_item.key == "s4hc_billto" -%}
{{metafields_item.value}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}