Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
Hello everyone, I'm trying to parse and save text data stored in Customer Notes, into several Metafields. For now I chose single line Metafields, Flow run history shows no error but unfortunately nothing happens.
Here is a snapshot of the flow:
Code:
{%- assign customernote_lines = customer.note | split: "<br />" -%} {%- for line in customernote_lines -%} {%- if line contains "City:" -%} {%- assign datacity = line -%} "{{- datacity | strip_newlines | strip -}}" {%- endif -%} {%- endfor -%}
Which is shown below in context:
Before step 4, a <br /> is added at the end of each line with newline_to_br so data looks like this:
Nickname: Rob<br />City: London<br />Country: United Kingdom<br />
Can you please advise about what's wrong with this code?
Solved! Go to the solution
This is an accepted solution.
Thanks for feedback, my bad some code was wrong so input data was empty.
Finally got it working removing step 3 to go directly to 4 and updating code a bit. Using a short wait step was a good suggestion, but not needed. Also make sure to click "Show all" Metafields in admin because when created by Flow, by default they're unpinned and lack definition - so they're hidden in UI.
Hey @Digico
The code you provided looks mostly correct for parsing and extracting the "City" information from the customer notes. However, it seems that you are not actually saving the extracted data into the Metafields.
To save the extracted data into the Metafields, you need to use the Shopify API or Shopify SDK to update the customer's Metafields. Here's an example of how you can modify your code to save the extracted "City" data into a Metafield:
{%- assign customernote_lines = customer.note | split: "<br />" -%}
{%- assign datacity = '' -%}
{%- for line in customernote_lines -%}
{%- if line contains "City:" -%}
{%- assign datacity = line | replace: "City:", "" | strip -%}
{%- endif -%}
{%- endfor -%}
{% comment %}
Assuming you have already set up the necessary Metafields for the customer, you can save the extracted city data like this:
{% endcomment %}
{%- assign namespace = 'your_namespace' -%}
{%- assign key = 'city' -%}
{%- assign value = datacity -%}
{% comment %}
Replace `customer.id` with the actual customer ID you are working with.
{% endcomment %}
{% shopify_api 'PUT', "/admin/api/2021-07/customers/{{ customer.id }}/metafields.json" %}
{
"metafield": {
"namespace": "{{ namespace }}",
"key": "{{ key }}",
"value": "{{ value }}",
"value_type": "string"
}
}
{% endshopify_api %}
In the above code, replace 'your_namespace' with the namespace you've set up for your Metafields, and 'city' with the key for the specific Metafield you want to store the city data in.
Make sure you have the necessary permissions to update Metafields using the Shopify API, and ensure that you are running this code in an appropriate context (such as a Shopify app or a custom Shopify script).
If I managed to help you then, don't forget to Like it and Mark it as Solution!
Best Regards,
Moeed
Thanks for the answer and time @Moeed , issue is question is about Shopify Flow App in this forum. Simply because in Shopify Flow App:
Customer Id aka {{ customer.id }} is already filled when you reach step 4, so it shouldn't need to repeat neither calling API (again). Shopify Flow uses GraphQL mutations to query API.
So I think the key problem where is that you are trying to update the note and then used the updated note in the next step. Flow pre-fetches all data used in a workflow at the start, in order to optimize API use. So it will use the old note in that metafield action. It you want to refetch that note, then you can add a short wait step before the metafield action. Anything after the wait step will be fetched after the wait step completes.
This is an accepted solution.
Thanks for feedback, my bad some code was wrong so input data was empty.
Finally got it working removing step 3 to go directly to 4 and updating code a bit. Using a short wait step was a good suggestion, but not needed. Also make sure to click "Show all" Metafields in admin because when created by Flow, by default they're unpinned and lack definition - so they're hidden in UI.
User | RANK |
---|---|
3 | |
1 | |
1 | |
1 | |
1 |
On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023You've downloaded the Search & Discovery app from the Shopify App store, and as you're ...
By Skye Nov 8, 2023The year-end shopping season is just around the corner. Is a flash sale on your radar? Are...
By Jasonh Nov 6, 2023