Hello,
I am building a post-purchase flow with some custom coding to personalize the language and images based on what the customer has ordered.
I am using the ‘for loop.first’ function within an if/else logic statement to accomplish this. I will paste the code below for reference, but basically if the items in the order meet the parameters of section one, the email should display section one’s text and pull in a dynamic image for that product.
My understanding of the forloop.first function is that the code should iterate through the loop of items purchased in an order, and should pull in the relevant image and text for the first item that meets the conditions set in the if/else statement.
However, when I have previewed some profiles on the flow email, the image and text are not always populating correctly. For example, when someone orders 3 items, and item number 1 and 3 do not meet the condition, but item 2 does, my preview email is showing me the fallback text and image in place rather than pulling in the dynamic image and text we set up within the logic statement.
I can’t figure out why this is happening. Orders where all items meet the condition are pulling in dynamic content correctly, and displaying the first item that meets the conditions.
Could anyone give me any guidance to troubleshoot this? Or any idea why this could be happening?
Thanks!
Here’s a look at our code for the text portion
{% for item in event.extra.line_items %}
<div> </div>
{% if 'bride' in item.product.tags and not 'father' in item.product.tags and not 'grandpa' in item.product.tags and not 'groom' in item.product.tags and not 'parents' in item.product.tags and forloop.first or 'mother of bride' in item.product.tags and forloop.first or 'mother of groom' in item.product.tags and forloop.first or 'mother of bride and groom' in item.product.tags and forloop.first or 'mother of bride or groom' in item.product.tags and forloop.first or 'bridesmaid' in item.product.tags and forloop.first or 'flower girl' in item.product.tags and forloop.first or 'grandma' in item.product.tags and forloop.first %}
<div>
<h3 style="text-align: center;"><span style="color: #393d40;"><span style="font-family: Lato, 'Century Gothic', CenturyGothic, AppleGothic, sans-serif; font-weight: 400; font-size: 34px;">You made a great choice {{ first_name|default:"friend" }}, she's going to love this gift 💕</span></span></h3>
</div>
{% elif 'groom' in item.product.tags and not 'bride' in item.product.tags and not 'mother' in item.product.tags and not 'grandma' in item.product.tags and not 'parents' in item.product.tags and forloop.first or 'grandpa of bride' in item.product.tags and forloop.first or 'grandpa of groom' in item.product.tags and forloop.first or 'best man' in item.product.tags and forloop.first or 'grandpa' in item.product.tags and forloop.first or 'grandfather of bride' in item.product.tags and forloop.first or 'groomsman' in item.product.tags and forloop.first or 'groomsmen' in item.product.tags and forloop.first %}
<div>
<h3 style="text-align: center;"><span style="color: #393d40;"><span style="font-family: Lato, 'Century Gothic', CenturyGothic, AppleGothic, sans-serif; font-weight: 400; font-size: 34px;">You made a great choice {{ first_name|default:"friend" }}, he's going to love this gift 💕</span></span></h3>
</div>
{% elif forloop.first %}
<div>
<h3 style="text-align: center;"><span style="color: #393d40;"><span style="font-family: Lato, 'Century Gothic', CenturyGothic, AppleGothic, sans-serif; font-weight: 400; font-size: 34px;">You made a great choice {{ first_name|default:"friend" }}, they're going to love this gift 💕</span></span></h3>
</div>
{% endif %}{% endfor %}
And for the dynamic image:
{% if 'klaviyo wedding not dad' in item.product.tags and forloop.first %}{% if item.product.variant.images.0.src %}{{item.product.variant.images.0.src}}{% else %}{{item.product.images.0.src|missing_product_image}}{% endif %}{% endif %}