Re: Add multiple values to a custom metafield | Flow Automation

Add multiple values to a custom metafield | Flow Automation

breaddddd
Shopify Partner
18 0 4

Hey there,

 

I am using Shopify's Flow to automate filling in a metafield for color to use this for a filter eventually.

In Flow I created the following automation:

Bildschirmfoto 2024-04-03 um 18.40.06.png

So whenever a new product is added to the store, it should check whether the value assigned to the metafield custom_label_2 is black or beige and add that value to another metafield called filters.farbe

 

This is how it looks inside that condition

Bildschirmfoto 2024-04-03 um 18.40.34.png

 

And this is how it looks inside the action: 

Bildschirmfoto 2024-04-03 um 18.41.50.png

 

The idea is that products which are both colours, black & beige, get those two values assigned to the new metafield filters.farbe. However, so far I only got one of those values added to that metafield.

 

As you can see, in the action field above, the type is "list of single line strings". For the metafield filters.farbe, the settings are also correct, as you can see here:

 

Bildschirmfoto 2024-04-03 um 18.40.52.png

 

The products that are assigned both colors are supposed to appear on both respective filter options (black & beige), if the color of that product includes those two colors. 

 

How is it possible? Am I doing the conditions in the Flow wrong? Currently, I always add "otherwise" conditions if it is not one of those colors. Should it actually be always a new condition string straight from the initial trigger?

 

Thank you very much in advance.

 

Replies 11 (11)

breaddddd
Shopify Partner
18 0 4

EDIT:
Sorry, there was a little mistake. Please switch the last and second last screenshot.

paul_n
Shopify Staff
1364 152 319

Yeah the way you set up the logic there, it will only run one of those actions and not both. Probably I would combine the conditions into a single step.

 

Because you are setting a list, I would then write the logic in liquid to check again for those colors. 

 

That said, managing lists is hard in liquid, so you could also use "Run code" to do that instead. A similar example is here: 

https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples/product-tags-to-metafields

 

See the index.js file for the main code.

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.
breaddddd
Shopify Partner
18 0 4

thank you so much for your help paul! Will update you as soon as I have implemented your approach!

breaddddd
Shopify Partner
18 0 4

UPDATE:
I tried the following flow:

Bildschirmfoto 2024-04-16 um 23.18.28.png


The code in "run code" is the following:

export default function main({product}) {
 // Exisitng Values in Metafield filters.farbe
 const filtersFarbeMetafieldObject = product.metafields.find((metafield) => metafield.namespace === "filters" && metafield.key === "farbe");
 const filtersFarbe = filtersFarbeMetafieldObject ? JSON.parse(filtersFarbeMetafieldObject.value) : [];
 const colorValues = ["schwarz", "beige", "braun", "weiß", "grau", "grün", "orange", "lila", "gelb", "blau", "rosa", "rot","bunt"]; // Die Farbwerte, die du überprüfen möchtest

 // Check whether the product label contains the colors 
 colorValues.forEach((colorValue) => {
   if (product.metafields.some((metafield) => metafield.namespace === "mm-google-shopping" && metafield.key === "custom_label_2" && metafield.value.includes(colorValue))) {
     if (!filtersFarbe.includes(colorValue)) {
       filtersFarbe.push(colorValue);
     }
   }
 });

 // Output of a string that can be directly transmitted to a metafield update action   return { filtersFarbe: JSON.stringify(filtersFarbe) };
}

 

 I ran the flow and got the following error message:

Bildschirmfoto 2024-04-16 um 23.23.27.png

 

I don't know much about programming, which is why I can't understand what I did wrong. 
From an other member of this forum I was told that the problem is the definition of my metafield "filters.farbe". It is currently not a JSON type metafield. 

So I deleted the old filters.farbe and created a new one and now I am stuck here:

Bildschirmfoto 2024-04-16 um 23.29.16.png

What goes into the field here now? Basically, it should contain all the possible colors (schwarz", "beige", "braun", "weiß", "grau", "grün", "orange", "lila", "gelb", "blau", "rosa", "rot","bunt") and allow a list of values in that metafield. For example, when there's a product that includes black and green color, both values should be sent to "filters.farbe" metafield for that product.


Thanks in advance.

paul_n
Shopify Staff
1364 152 319

That error is likely because your input query does not contain the product or the metafields you are checking. What is in it?

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.
breaddddd
Shopify Partner
18 0 4

Thank you for the reply.

 

I am sorry, Paul. I am not sure what exactly you mean with input query. Where do I need to check what and send you a screenshot from?

paul_n
Shopify Staff
1364 152 319

There is a box labeled define inputs:

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.
breaddddd
Shopify Partner
18 0 4

Thank you Paul. Here is the requested screenshot:

Bildschirmfoto 2024-04-17 um 13.04.22.png

I see that I have not defined any in or outputs. What do I need to put in there? 

paul_n
Shopify Staff
1364 152 319

Any field you use in the code part needs to be accessed via the input query. Something like:

{
  product {
    metafields {
      namespace
      key 
      value
    }
  }
}

 

Your code should return something...probably a string if you want to set a metafield with it. Message might be the wrong name so you can change that to match what it contains. 

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.
breaddddd
Shopify Partner
18 0 4

Hey Paul,

 

i am very sorry for the late reply, but I actually contacted the developer team of the page and they fortunately found a different way to this problem. We just created a new metafield that captured the values of an other metafield as list values and added "multicolor" additionally when the product has more than 1 color. 

 

Still, I want to thank you for your help! I appreciate the effort and time you put into this!

paul_n
Shopify Staff
1364 152 319

Sounds good, glad you found a solution

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.