How do I send multiple values to a metafield where some are valid and some are invalid

How do I send multiple values to a metafield where some are valid and some are invalid

Ashley_Kennerle
Shopify Partner
58 0 8

I want to create a Search & Discovery filter that uses some but not all of my product tags.

 

Since this is not possible to do directly (there's no way to exclude the product tags that I want to hide), I thought that I would make a metafield of single line text with multiple values with a list of the tags that I want to use in the validations part.

 

I've modified the "Convert tags with a prefix to a product metafield using the Run code action" Flow template to remove the colour prefix part and I can pass the tags into the metafield successfully.

 

The problem I'm having is that if there are tags that I want to exclude, then the whole update fails.

 

For example, in the metafield if I allow the values 'AAA' and 'BBB' but I run the flow on a product with tags 'AAA' and 'CCC', I was hoping that 'AAA' would be accepted and added to the product, and 'CCC' would be rejected.

 

Unfortunately if there are any tags that aren't in the valid list, the whole update fails.

 

Does anybody know if there's a way for me to achieve what I'm trying to do?

Replies 12 (12)

paul_n
Shopify Staff
1339 151 310

Should be possible, but you need to post more details of your existing 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.
Ashley_Kennerle
Shopify Partner
58 0 8

Thanks Paul, here are some screenshots of my solution, let me know if there's anything else you need to know.

 

Screenshot 2024-07-01 at 15.46.06.png

Screenshot 2024-07-01 at 15.47.23.png

 

Screenshot 2024-07-01 at 15.47.41.png

This works:

 

Screenshot 2024-07-01 at 15.49.32.png

Screenshot 2024-07-01 at 15.50.16.png

 

If I remove 'Reggae' as one of the valid values and run it again, I get this:

 

Screenshot 2024-07-01 at 15.51.19.png

 

Screenshot 2024-07-01 at 15.52.11.png

paul_n
Shopify Staff
1339 151 310

Oh, I see now. Flow doesn't have any control over that validation so that best you can do is workaround it. You could check if the list you want to insert is defined in the metafield / metafieldDefinitions / validations / values field

 

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.
Ashley_Kennerle
Shopify Partner
58 0 8

I'm happy to do a workaround (this whole thing is a workaround for Search & Discover!) - do you have any examples to explain what you are suggesting please?

paul_n
Shopify Staff
1339 151 310

I don't as this is pretty unusual use case. I think you would want to add that logic to your Run code action. 

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.
Kalen_Jordan
Shopify Partner
758 34 135

Yeah if you just add that logic into your run code step you should be good to go. 

Ashley_Kennerle
Shopify Partner
58 0 8

Thanks Kalen. I'm very new to using Flow, please could you point me in the direction of a code example that would do something similar to what you're suggesting?

 

I don't know how to get the metafield / metafieldDefinitions / validations / values field.

 

Do I have to add it to the input query?

 

query {
product {
tags
metafields {
namespace
key
value
}
}
}

paul_n
Shopify Staff
1339 151 310

Inside that metafields, you would type metafieldDefinitions and it should show you that as an option.  Each means one level deeper and needs to be surrounded by { } .

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.
Ashley_Kennerle
Shopify Partner
58 0 8

Thanks Paul - I'm getting this error - am I putting it in the right place?

 

Screenshot 2024-07-02 at 13.49.44.png

paul_n
Shopify Staff
1339 151 310

Sorry the type is named metafieldDefinition (singular), but it's referenced by "definition"

  product {
    metafields {
      definition {
        validations {
          value
        } 
      }
    }
  }

 

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.
Ashley_Kennerle
Shopify Partner
58 0 8

Thanks so much Paul, I've updated the javascript - here it is in case anybody has the same question as me in future:

 

/**
 * Convert tags to list of metafields
 * Convert product tags with a certain prefix into a product metafield of type list.single_line_text_field
*/

export default function main({product}) {
  // Change these values to match your use case
  const metafieldNamespace = "custom";
  const metafieldKey = "filter_tags";

  // Get the existing values in the metafield custom.filter_tags
  const colorMetafieldObject = product.metafields.find((metafield) => metafield.namespace === metafieldNamespace && metafield.key === metafieldKey);
  const colorMetafield = colorMetafieldObject ? JSON.parse(colorMetafieldObject.value) : [];
  
  const validGenres = JSON.parse(colorMetafieldObject.definition.validations[0].value);
  
  product.tags.forEach((tag) => {
    if (!colorMetafield.includes(tag) && validGenres.includes(tag)) {
       colorMetafield.push(tag);
    }
  });

  //Output a string that can be passed directly to an update metafield action
  return { colorMetafield: JSON.stringify(colorMetafield) };
}
Kalen_Jordan
Shopify Partner
758 34 135

I'm a little short on time right now but if you're interested in working with me on a paid basis, I have a link in my footer signature here.