Solved

Forloop.first not functioning properly within if/else logic - django

  • 18 January 2024
  • 3 replies
  • 97 views

Badge

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 %}

icon

Best answer by Brian Turcotte 8 February 2024, 19:56

View original

3 replies

Userlevel 7
Badge +36

Hi @Angie.lynn.marketing!

I’m going to check on this internally and I’ll update the thread ASAP!
 

- Brian

Userlevel 7
Badge +36

Hi @Angie.lynn.marketing!

I believe the issue could be in your dynamic image logic, where you're only checking for one specific condition ('klaviyo wedding not dad' in item.product.tags).

 

Try this for the dynamic image code:

{% for item in event.extra.line_items %}
{% if 'klaviyo wedding not dad' in item.product.tags %}
{% if item.product.variant.images.0.src %}
{{ item.product.variant.images.0.src }}
{% else %}
{{ item.product.images.0.src | missing_product_image }}
{% endif %}
{% break %}
{% endif %}
{% endfor %}

 

Best,

Brian

 

Badge

Hi Brian,

Thanks for taking a look, and for your response! I tried the code you suggested for the dynamic image, but with the {% break %} line in place I received the “Message displayed without tags or variables” error upon previewing the template with dynamic block.

After playing around a bit with it, I removed the {% break %} and the block did in fact pull in the correct product, but displayed the image twice - even when the order had only one of those items.

 

So I’m still looking for a workaround to pull in the first item that meets our conditions and display just that image.

 

Klaviyo support took a look at our initial issue as well and suggested that the platform’s django capabilities might not support this specific customization we are trying to do right now. 

Reply