Solved

Django - need help refining if/else logic when if condition is true more than once

  • 4 October 2023
  • 6 replies
  • 145 views

Badge +3

 

Hello,

 

I am working on an if/else logic statement to display targeted text to a customer who has purchased an item within a specific collection. We are identifying items in this collection with the product tag ‘klaviyo wedding’.

 

Below is the statement I created, which is working correctly - displaying the text and pulling in the item name when a product tagged ‘klaviyo wedding’ is purchased:

 

{% if 'klaviyo wedding' in item.product.tags %}
<div style="text-align: center;"><span style="font-size: 22px;"><strong>Share The Love With The Whole Wedding Party!</strong></span></div>
<div style="text-align: center;"><span style="font-size: 22px;"><br />We're working on your {{ item.product.title }}! Before we wrap and ship them, is there anyone else we can help you cross off your gift list?</span></div>
{% endif %}

 

However, my problem is that when more than one item with that tag are purchased in the same order, the text displays multiple times - once for each item:

 

I need the text to appear only once - not for every item purchased that matches the tag. It isn’t important which specific item in the order displays, just that we aren’t replicating the message for every item purchased. 

 

Is there a way to write this statement that could limit the number of items it displays for, thereby only displaying one time, for one of the items in the order?

Thank you!

icon

Best answer by KeviSunshine 7 October 2023, 21:07

View original

6 replies

Userlevel 4
Badge +7

Hi @AKwaterbody,

I’m not sure what your template looks like, but it appears to me that you must be adding this code in a “loop.”

I’m not too familiar with Klaviyo loops in templates, but I know they have support for this and the main use case is looping through every item (in an order) and displaying the text.

Your code must be looping through each product and displaying this same text.

Do you know what I’m referring to? Can you check for this?

 

Cheers,

Kevin.

Badge +3

Hi Kevin,

Yes, to be more specific I am adding this code inside of a dynamic table, set up as pictured below:

 

So maybe because I am asking it to loop over all products in the order, it is displaying the text every time a product meets the ‘if’ condition? Is there a way to limit the number of messages produced to just display once within this context?

Thanks for taking a look!

Badge +3

Edit: I think I’ve solved my problem! Your reply and comment about the ‘loop’ got me thinking about my problem a little differently, and I realized I need to be telling Klaviyo to limit or filter the output from the input, so to speak:

 



Rather than trying to filter the output from within the ‘if/else’ statement; I added the ‘slice’ filter on to our row collection variable under dynamic table settings, and sure enough that applied the logic to the first item in the list of products that met the specified conditions. I used this on both the text and image dynamic blocks and it appears to be working as needed. 

Leaving this all here in case someone runs into the same quandary as I did (:

Thanks,

Angie

Userlevel 4
Badge +7

Hey Angie,

Well done! Slice is a helpful tool!

I do have one question: if you slice the line items, you’ll only receive the first element, rather than the first element that meets the “if” condition.

So if your “if” condition is “item is a wedding party,” the “wedding party” item would have to be the first item in your list of items.

I may be mistaken with how your data works, or maybe the “if” statement would always relevant on the first item (i.e., it’s on all items, so checking the first item is sufficient).

 

Hope you’ve solved it–just want to make sure your solution works in all of your scenarios!

 

Cheers,

Kevin.

Badge +3

Hi Kevin,

No, you’re right 😢 this wasn’t quite the solution I was looking for! I previewed a few profiles and it is just taking the first item, so it won’t always work depending on the arrangement of items ordered. So it looks like I’m back to square one. Any ideas on how to apply this same functionality - selecting the first item that meets the if condition - within the if/else statement? I couldn’t get the slice function to work properly within the conditional logic before. 

Thanks!

Userlevel 4
Badge +7

Hi @AKwaterbody,

 

No problem, I’m glad you were able to test it at least.

I am not able to find a good filter function right now that supports this, but I know for sure that one exists. I am not a Django filter expert, and Klaviyo’s filter/Django documentation is severely lacking (sorry if that’s harsh! Just my opinion!).

 

I’ll loop in @David To who is a really helpful person here–David, could you get a Klaviyo developer or try to enlist a Django community expert to help here? What we’re looking for is an “array-find” function to loop through an array of items and check if any have a certain property.

Angie–you can always reach out to Klaviyo email support too. They’re super helpful and have helped me with similar Django functions in the past.

 

Cheers all,

Kevin.

Reply