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

4 comments
Date Votes
  • Official comment

    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:

    {{#contains (regexMatch tags "(?:^|\s)([Bb]lue)(?=\s|,|$)") "Blue"}}true{{else contains (regexMatch tags "(?:^|\s)([Bb]lue)(?=\s|,|$)") "blue"}}true{{else}}false{{/contains}}

    If the value you're searching will always be "Blue" with an uppercase "B", it can be simplified to the following:

    {{#contains (regexMatch tags "(?:^|\s)(Blue)(?=\s|,|$)") "Blue"}}true{{else}}false{{/contains}}

    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:

    1. Blue is at the beginning of the string
    2. Blue is in the middle of the string
    3. Blue is at the end of the string
    {{#compare (split tags "," 0) "==" "Blue"}}true{{else}}{{#compare (regexSearch (join "" tags ",") ", Blue,") ">=" 0}}true{{else}}false{{/compare}}{{/compare}}

     

    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!

    1
  • 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:

    {{#contains (split tags "," 0) "Gold"}}
    Gold
    {{else}}
    {{#compare (regexSearch (join "" tags ",") ", Gold,") ">=" 0}}
      Gold
    {{else}}
      {{#contains (split tags "," 0) "Silver"}}
        Silver
      {{else}}
        {{#compare (regexSearch (join "" tags ",") ", Silver,") ">=" 0}}
          Silver
        {{else}}
          {{#contains (split tags "," 0) "Bronze"}}
            Bronze
          {{else}}
            {{#compare (regexSearch (join "" tags ",") ", Bronze,") ">=" 0}}
              Bronze
            {{/compare}}
          {{/contains}}
        {{/compare}}
      {{/contains}}
    {{/compare}}
    {{/contains}}

    ...yikes!

    1
  • 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.

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post