show/hide template block based on product bought

  • 19 January 2022
  • 6 replies

Badge +2

I’d like to show specific content only for customers who have purchased a specific item, using the following method: How to Show or Hide Template Blocks Based on Dynamic Variables – Klaviyo - Help Center

What would be the condition structure for the variable ‘has purchased SKU x’? 




Best answer by alex.hong 24 January 2022, 21:32

View original

6 replies

Userlevel 7
Badge +58

Hi there @Leo Pure Electric,

Thanks for posting your question to the Community!

If you wanted to show or hide blocks based on event data (like purchasing a specific SKU), event.SKU (replace the SKU with the product SKU). If you would like to filter by Product name, then the following condition will need to be applied: event.ProductName.

As a note, unfortunately, we are not able to iterate multiple items, while using the hide/show logic. This means we would need to manually pull in each node to see if the condition is true or false.
An example of the condition would look like:

event.extra.line_items.0.product.variant.sku == '100' or event.extra.line_items.1.product.variant.sku == '100' or event.extra.line_items.2.product.variant.sku == '100' 

The problem is, the recipient could have more than 10 items so your conditional statement would be extremely long to catch all the SKUs you are searching for. But that is the only way at this time.

Hope this helped!

Userlevel 2
Badge +9

Hey @Leo Pure Electric

As ​​​@alex.hong pointed out above, it will be tricky to show/Hide product blocks based on the Product SKU because a customer might add multiple products to the cart and it would make the conditional statement really long and confusing. 

However, you can achieve that show/hide functionality by using the Product Name instead of SKU. 

In that case, the Show/Hide condition becomes:
‘Product Name’ in {{event.Items|join:”,”}}

Here, all the product names are joined together with a comma and then it matches for ‘Product Name’ in the final output. Also, this will stay independent of how many items were added by the user to the cart or purchased by the user. 

Thank you, 


Badge +2

Thanks a lot both of you for your answers.


Would I be able to somehow show/hide the content based on both what product they purchased (so using one of the method above) AND if they are opted-in to marketing? 


For context, this is my use case: I’m sending how-to guides for our products which does not require marketing consent. As part of these emails, I’d like to have up-sell content block, ideally taking into account what they purchased before (so we avoid trying to sell to them something they already bought). We obviously can only show this up-sell block to those opted-in to marketing, hence the ask for the dual condition to show/hide content. 



Userlevel 7
Badge +58

Hi there @Leo Pure Electric,

Glad you found the info helpful! 

If you wanted to show or hide a block based on if they accept marketing, that would look like “{{ person|lookup:'Accepts Marketing'|default:'' }}”

In our HC documentation regarding template blocks and dynamic variables, there is a section you might find helpful related to Profile Data and multiple conditions. I would recommend looking through those sections and modify them as you want to tackle on the problem for your previous post. 


Badge +2

Thanks a lot. 

I understand this topic a bit more but still struggling to build what I’m after. 

I want to be able to show a specific content to those who are opted-in to marketing and have purchased an item from a specific collection (helmets). 

My condition is like this for the moment: {{ person|lookup:'Accepts Marketing'|default:'' and XXXX }}

I cannot figure out what to add to only include those who have NOT purchased the item from this specific collection. 

Help :) 

Userlevel 7
Badge +58

Hi @Leo Pure Electric,

Thinking through your scenario do you mind if I share an alternative approach to this? 

Would you mind letting me know a little more about the flow you are looking to set up or a link to the flow if it's already been set up?

It would depend on what type of trigger is used to set up the flow. The type and amount of customer dynamic information is stored in the metric that is used to trigger, so you'll be able to pull in different information for different metrics. If it's a list based flow, you'll only be able to pull in customer properties and not dynamic content from actions they've taken. 

I would actually recommend using a conditional statement within your flow for this email. Could you try use a conditional split and have a separate email for users who have purchased vs those who haven't? I feel like this might be the better solution than trying to create AND statements. We have a section in our Guide to Creating a Welcome Series that shows how to set this up.

Let me know what you think.