Skip to main content

Shopify Customer Tag Evaluation via Handlebars

Comments

4 comments

  • Official comment
    Dan Claypool Support manager NetSuite Integration Whiz
    Great Answer
    Celigo University Level 4: Legendary

    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).

  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    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
  • Justin Bodin
    Celigo University Level 4: Legendary
    Great Answer
    Engaged

    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
  • Tyler Lamparter Principal Product Manager
    Awesome Follow-up
    Engaged
    Top Contributor
    Answer Pro
    Celigo University Level 4: Legendary

    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.