Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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?
Should be possible, but you need to post more details of your existing solution.
Thanks Paul, here are some screenshots of my solution, let me know if there's anything else you need to know.
This works:
If I remove 'Reggae' as one of the valid values and run it again, I get this:
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
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?
I don't as this is pretty unusual use case. I think you would want to add that logic to your Run code action.
Yeah if you just add that logic into your run code step you should be good to go.
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
}
}
}
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 { } .
Thanks Paul - I'm getting this error - am I putting it in the right place?
Sorry the type is named metafieldDefinition (singular), but it's referenced by "definition"
product {
metafields {
definition {
validations {
value
}
}
}
}
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) };
}
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.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024