Skip to main content
Contributor I
November 3, 2021
Solved

Cart Abandonment Search for Variant/Master Product Prices in For Loop

  • November 3, 2021
  • 3 replies
  • 309 views

I have a loop in my Cart Abandonment that iterates through all items in the cart. ex - {% for item in event.Items %}{% endfor %}

I'm trying to find a way to check if each product has a {{item.Variant.Price }}, if it doesn't have a variant price I would like to show the master price or {{item.Price }}.

I was trying to write an if statement, but I wasn't able to get it working. Is there a way to do this?

    This topic has been closed for replies.
    Best answer by alex.hong

    Hey there @oeainteractive ,

    Welcome to the Community and thanks for posting! I hope to provide with some help on this.

    So I believe the method to search for the item variants and item master price would be 

    {% for item in event.Items %}
    {% if item.Variant.Price %}{{ item.Variant.Price }}{ % else %}{{ item.Price }}{% endif %}{% endfor %}

    Please let me know if that worked!
    Alex

    3 replies

    alex.hong
    alex.hongAnswer
    Klaviyo Alum
    November 3, 2021

    Hey there @oeainteractive ,

    Welcome to the Community and thanks for posting! I hope to provide with some help on this.

    So I believe the method to search for the item variants and item master price would be 

    {% for item in event.Items %}
    {% if item.Variant.Price %}{{ item.Variant.Price }}{ % else %}{{ item.Price }}{% endif %}{% endfor %}

    Please let me know if that worked!
    Alex

    Contributor I
    November 8, 2021

    Hi Alex,

    I tried the code you suggested, but I’m running into an issue. Take a look at this example that is iterating through four different products. This is a preview of the last two products...

     

     

    alex.hong
    Klaviyo Alum
    November 8, 2021

    Hey there @oeainteractive,

    Thanks for writing back! 

    There could be a couple of reasons why this did not end up working for you.

    First, the code I provided will only work if there is a price value coming into klaviyo with the triggering event. If your triggering event does not send a price value into klaviyo, then the code would not work. I am assuming that your “Price” event is not pulling in data/does not have data to pull so it will not work with the code. From the community side, we cannot take a deeper dive into your account to see which variables are or are not getting pulled in. Ultimately I would advise to take a look into our guide to find the correct event variable to refer to.

    Another thing I notice is that the syntax you have presented is not technically accurate. It would need to be {% else if %} for multiple else cases but you are using {% else %}. 

    {% for item in event.Items %}{% if item.Price %}{{ item.Price }}{ % else if %}{{ item.RowTotal }}{ % else if %}{{ VALUE }}{ % else if %}{{ VALUE }}{ % else %}{{ VALUE }}{% endif %}{% endfor %}

    As you can see, only the last else statement can be “else”. All other "else" statements before it need to be {% else if %}. But ultimately, if you are custom coding HTML, then this should be the same for all cases and standard practice. 

    Lastly, I would recommend reaching out to support if you would want a more in depth look into your account!
    Thanks,

    Alex