Forum Discussion

elena14's avatar
elena14
Practitioner
5 months ago

Loop through a catalog to display the product a user has as a custom attribute

Hello, 

So I am trying to display some Liquid logic into an email template. The idea is that I have a catalog named house_properties with fields: id, name and description.

Each user has a custom attribute called user_product_id which we should use in the Liquid code to display the name of the product from the catalog if the two id's are the same.

This is the Liquid code I have been using, but apparently is not working. Can someone please let me know what am I missing here?

 

 

{% assign user_product_id = {{ custom_attribute.${user_product_id} }} %}

<!-- Loop through each row in the catalog -->
{% for row in house_properties %}
  <!-- Loop through each item in the current row -->
  {% for item in row %}
    <!-- Check if the item name contains the user's custom attribute -->
    {% if item.id == {{ custom_attribute.${user_product_id} }} %}
     {{ item.name }}
    {% endif %}
  {% endfor %}
{% endfor %}

 

 

Thank you in advance!

Elena

  • DavidO's avatar
    DavidO
    Strategist II

    Hi elena14 

    I'm assuming the house properties catalog is assigned early on so the loop knows which catalog to loop through.

    Otherwise this looks pretty good to me except I can't see where you are using the assigned user_prodcut_id? Should it be part of your IF statement rather than the custom attribute liquid so the code would look like this:

     

    {% assign user_product_id = {{ custom_attribute.${user_product_id} }} %}
    
    <!-- Loop through each row in the catalog -->
    {% for row in house_properties %}
      <!-- Loop through each item in the current row -->
      {% for item in row %}
        <!-- Check if the item name contains the user's custom attribute -->
        {% if item.id == user_product_id %}
         {{ item.name }}
        {% endif %}
      {% endfor %}
    {% endfor %}

     

  • Hi DavidO ,

    Actually I am not sure which is the correct way to assign the correct catalog, so we know which catalog we are looping through.

    According to Braze documentation this is the code: 

    {% catalog_items house_properties %} 

    But this is not working with the full code above. Can you please advice how we can actually loop through the catalog items? Thanks!

    • StratosK's avatar
      StratosK
      Active Member

      Hello elena14 
      I am trying to do something similar with what you described above. Is there any chance that you have found a way to use a catalog in a for loop ?

      • rayelkho's avatar
        rayelkho
        Practitioner III

        I wonder if you need to Catalog Selections and loop through that