Nested for loop creating duplicate values

Highlighted
Tourist
5 1 1

Scenario:

We have installed an app called Reorder Master which displays a reorder button in the order history table on the customer account page. We have a few products that can only be ordered once by the customer. So for these products we wish to hide this reorder button. The following code correctly hides the reorder button for these products, but it duplicates the reorder button for the others.

{% for order in customer.orders %}
              <tr>
                <td>{{ order.name | link_to: order.customer_url }}</td>
                <td>{{ order.created_at | date: format: 'date' }}</td>
                <td>{{ order.financial_status_label }}</td>
                <td>{{ order.fulfillment_status_label }}</td>
                <td>{{ order.total_price | money }}</td>
                <td>
                  {% for line_item in order.line_items %}
                  	{% if line_item.title contains "Introductory" %}
                  		<div></div>
                  	{% else %}
                  		<div class="hulk_reorder_button" onclick="Hulkappreorder({{ order.id }})"></div>
                  	{% endif %}
                  {% endfor %}
                </td>
              </tr>
            
{% endfor %}

 

Here is what it currently looks like:

703af4faeb497621d788ec9ab78788b4.png

 If anyone could help us fix this issue, that would be greatly appreciated, thanks.

0 Likes
Highlighted
Tourist
5 1 1

Realised that the original approach was outputting a reorder button for every order line. The following code is the solution to this problem:

{% for order in customer.orders %}
              <tr>
                <td>{{ order.name | link_to: order.customer_url }}</td>
                <td>{{ order.created_at | date: format: 'date' }}</td>
                <td>{{ order.financial_status_label }}</td>
                <td>{{ order.fulfillment_status_label }}</td>
                <td>{{ order.total_price | money }}</td>
                <td>
                  
                  {% assign var1 = "" %}
                  {% assign var2 = "" %}
                  {% for line_item in order.line_items %}
                  	{% if line_item.title contains "Introductory" %}                  
                  		{% assign var1 = "true" %}                  		
                  	{% else %}
                  		{% assign var2 = "false" %}            		
                  	{% endif %}
                  {% endfor %}
                  
                  {% assign combine = var2 | append: var1 %}
                  {% if combine contains "true" %}
                  	<div></div>
                  {% else %}
                  	<div class="hulk_reorder_button" onclick="Hulkappreorder({{ order.id }})"></div>
                  {% endif %}
                </td>
              </tr>
            
            {% endfor %}

 

 

1 Like