2

I am trying to print the unique items based on their variant ids. Only unique items are to be printed.

{% set unique_items = [] %}
{% for item in items %}
    {% if item.variant_id not in unique_items %}
         {% set unique_items = unique_items + [item.variant_id] %}
             {% set quantity = items | selectattr("variant_id", "equalto", item.variant_id) 
                        | list | length %}
             <tr>
             <td style="width: 0; padding: 0px !important">
                 <img
                     src="{{ item.image_url }}"
                     style="height: 118px; width: 78px; border-radius: 4px"
                 />
             </td>

...

Here the code is trying to print image of only unique items that have different variant_ids.

I have tried this but it prints all the items. I don't want repeating items.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Does this answer your question? [Change the value of a variable inside a loop](https://stackoverflow.com/questions/9486393/change-the-value-of-a-variable-inside-a-loop) – Tsyvarev Jun 28 '23 at 09:28
  • 2
    Jinja has filter [unique](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.unique) which could be used instead of manual filtering: `{% for item in items | unique(attribute='variant_id') %}`. – Tsyvarev Jun 28 '23 at 09:31

0 Answers0