How to add a metafield automatically based on product tags

Topic summary

A user needed to automatically populate a product’s “size” metafield based on incoming product tags (“medium”, “Large”, “Huge”, or “Gargantuan”) when products are pushed from an external system to Shopify.

Solution Provided:

  • Use Shopify Flow app to automate the process
  • Import a custom workflow that triggers when products are added
  • The workflow checks product tags and sets the corresponding metafield value

Troubleshooting Steps:

  1. Initial flow file provided but needed customization to select the correct “Size” metafield
  2. First error: Extra newline characters (\n) in the value field caused validation errors
  3. Code refinement: Removed newline characters from the Liquid template code using {%- -%} syntax
  4. Final error was user-side, likely from incorrect code copying

Resolution:
The issue was resolved after multiple iterations of refining the Liquid code in the Flow’s value field. The final working code properly strips whitespace and newlines, allowing the metafield to populate correctly with just the size value (e.g., “Medium”) based on the product’s tag.

Summarized with AI on October 29. AI used: claude-sonnet-4-5-20250929.

Hey all, I use an external solution to set up my products, and then push those products to my shopify store. What I need, is to automate the following process-

A product is added to my catalogue containing a certain tag “medium”, “Large” “Huge” or “Gargantuan”. This is already in place as the product enters shopify.

Then, I have a metafield set up for “size” already.

I need the product to automatically add a metafield to “size” as “medium” “Large” Huge" or “Gargantuan” based on what the incoming product is tagged with.

How can I achieve this functionality?

Hi @Bonusfeatures75 , you can probably do this with Shopify Flow. I created this Flow but haven’t tested it yet. Please install the Shopify Flow app. Open the app and click on Import and import the attached .flow file.

Then test with a product. Hopefully this will work. If not, please let me know.

Actually, Shopify won’t let me upload the flow file; so here’s what you need to do;

  1. open any code editor or text editor

  2. paste the following code in it

  3. save the file, name it something like update-product.flow

  4. then import it to Shopify Flow

3b72422d2cc12789299b4ef183fa92df029d058361196983a5d81be5d7844c78:{"__metadata":{"version":0.1},"root":{"steps":[{"step_id":"3feaebe0-14b2-11f0-b87c-255a5e178b59","step_position":[0,0],"config_field_values":[],"task_id":"shopify::admin::product_added","task_version":"0.1","task_type":"TRIGGER","description":null,"note":null,"name":null},{"step_id":"ab1e28a0-14b2-11f0-b87c-255a5e178b59","step_position":[360,0],"config_field_values":[{"config_field_id":"condition","value":"{\"uuid\":\"b9f830f7-14b2-11f0-b87c-255a5e178b59\",\"lhs\":{\"uuid\":\"b9f830f0-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f7-14b2-11f0-b87c-255a5e178b59\",\"array_path\":{\"uuid\":\"b9f830f1-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f0-14b2-11f0-b87c-255a5e178b59\",\"value\":\"product.tags\",\"comparison_value_type\":\"EnvironmentValue\"},\"array_item_key\":{\"uuid\":\"b9f830f2-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f0-14b2-11f0-b87c-255a5e178b59\",\"value\":\"tags_item\",\"comparison_value_type\":\"EnvironmentValue\"},\"operation\":{\"uuid\":\"b9f830f3-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f0-14b2-11f0-b87c-255a5e178b59\",\"lhs\":{\"uuid\":\"b9f830f4-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f3-14b2-11f0-b87c-255a5e178b59\",\"lhs\":{\"uuid\":\"b9f830f5-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f4-14b2-11f0-b87c-255a5e178b59\",\"value\":\"tags_item\",\"comparison_value_type\":\"EnvironmentValue\",\"full_environment_path\":\"tags_item\"},\"rhs\":{\"uuid\":\"c9da8180-14b2-11f0-b87c-255a5e178b59\",\"parent_uuid\":\"b9f830f4-14b2-11f0-b87c-255a5e178b59\",\"value\":\"[\\\"medium\\\",\\\"Large\\\",\\\"Huge\\\",\\\"Gargantuan\\\"]\",\"comparison_value_type\":\"LiteralValue\"},\"value_type\":\"EnvironmentScalarDefinition:String\",\"operator\":\"in?\",\"operation_type\":\"Comparison\"},\"operator\":\"AND\",\"operation_type\":\"LogicalExpression\"},\"operator\":\"ANY\",\"operation_type\":\"ArrayExpression\"},\"operator\":\"AND\",\"operation_type\":\"LogicalExpression\"}"}],"task_id":"shopify::flow::condition","task_version":"0.1","task_type":"CONDITION","description":null,"note":null,"name":null},{"step_id":"e7047a90-14b2-11f0-b87c-255a5e178b59","step_position":[720,0],"config_field_values":[{"config_field_id":"product_id","value":"product.id"},{"config_field_id":"metafield","value":"{\"namespace\":\"test_data\",\"key\":\"binding_mount\",\"type\":\"single_line_text_field\"}"},{"config_field_id":"value","value":"{% if product.tags contains 'medium' %}medium{% endif %}\n{% if product.tags contains 'Large' %}Large{% endif %}\n{% if product.tags contains 'Huge' %}Huge{% endif %}\n{% if product.tags contains 'Gargantuan' %}Gargantuan{% endif %}"}],"task_id":"shopify::admin::add_product_metafield","task_version":"1.0","task_type":"ACTION","description":null,"note":null,"name":null}],"links":[{"from_step_id":"3feaebe0-14b2-11f0-b87c-255a5e178b59","from_port_id":"output","to_step_id":"ab1e28a0-14b2-11f0-b87c-255a5e178b59","to_port_id":"input"},{"from_step_id":"ab1e28a0-14b2-11f0-b87c-255a5e178b59","from_port_id":"true","to_step_id":"e7047a90-14b2-11f0-b87c-255a5e178b59","to_port_id":"input"}],"patched_fields":[],"workflow_name":"Update product metafield for certain tags"}}

This does not appear to have worked. The “Size” metafield on the product remains blank

This does not appear to have worked. The “Size” metafield on the product remains blank

Oh wait, I forgot. Since I don’t have a “Size” metafield, you’ll have to edit the flow, edit the last step and select the “Size” metafield, then save.

I am now getting this error - Exception: Got error updating metafield: “Value must be a single line text string.” For value: “Medium\n\n\n”, Type: single_line_text_field

That’s good progress. Okay, so now, in the last step where you updated the metafield, now update the value field below the metafield selection. It’ll contain some code, remove all the code and add this code, make sure there’re no empty new lines or spaces.

{%- if product.tags contains 'medium' -%}medium{%- endif -%}{%- if product.tags contains 'Large' -%}Large{%- endif -%}\n{%- if product.tags contains 'Huge' -%}Huge{%- endif -%}\n{%- if product.tags contains 'Gargantuan' -%}Gargantuan{%- endif -%}

We’re so close! This worked, but resulted in the metafield reading “Medium\n\n” Instead of “Medium” Like it would need to be to function.

{%- if product.tags contains 'medium' -%}medium{%- endif -%}{%- if product.tags contains 'Large' -%}Large{%- endif -%}{%- if product.tags contains 'Huge' -%}Huge{%- endif -%}{%- if product.tags contains 'Gargantuan' -%}Gargantuan{%- endif -%}

My bad, I forgot to remove some of the new lines auto created in the original code. Here’s the updated one.

New error now- Exception: Got error updating metafield: “Value can’t be blank.” For value: “”, Type: single_line_text_field

edit- this was a mistake on my end. Its working now! Thank you so much!