Shopify Customer Tag Evaluation via Handlebars
Hey all,
I'm running a custom flow and trying to evaluate a Shopify customer's tags and set a NetSuite field based on the results.
The tags data is a comma separated string, so if customer has tags: Red, Green, Blue-Green, then the JSON is:
tags: "Red, Green, Blue-Green"
In my case, I need to check if the exact tag "Blue" exists and set a NetSuite field accordingly.
I was using #contains, but it's not exact enough, for example the following evaluates to "Blue" because of the "Blue-Green" tag:
{{#contains tags "Blue"}}Blue{{else}}{{/contains}}
If I had an object to iterate through, that would make this simple. But I can't think of how to do it given the string data.
Any help/idea are appreciated, thank you!
0
Comments
Hi Justin,
It appears that Tyler's expression might give a false positive on "Blue-green" if that value is in the first position of the comma-separated string.
If you need a case-insensitive expression, this one may better suit your needs:
If the value you're searching will always be "Blue" with an uppercase "B", it can be simplified to the following:
Here's a screenshot of some tests I ran with Tyler's expression and mine:
For an explanation of the regexMatch expression, click here (this will take you to a new site).
Justin Bodin, Ideally I could have figured out a regular expression, but that's a problem in and of itself. I made this for you and it should work for each of the following scenarios:
It first uses a contains expression to check if the first string is set to Blue. It does this by using the split function to split on comma and then return the first position of the array split. The next part checks if ", Blue," is within the string. If it is, then it returns the position of the matched string, if there is no match it returns -1. In order to check the last word in the string, I first added a comma to the end of the string so that ", Blue," would match any case within the middle or at the end.
Hopefully this helps!
Yes! You're awesome, Tyler. Thank you! That works perfectly. Thanks for lending your creativity.
I need to check for 3 different values (more than 1 can never exist simultaneously) and output it if it exists. So my final solution was as follows, in case it's helpful for someone coming across this in the future:
...yikes!
It looks like I stand corrected. If you change the contains in my original expression to a compare statement, then it works as well. Dan Claypool's regex only method is much cleaner than my brute force haha. I changed my original comment as not to confuse future others.
Please sign in to leave a comment.