Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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:
Basically in my code after an Order is created it will do an Update Order Metafield action:
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:
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
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 -%}
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?
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 -%}
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024