<p>You want to use the <a href="https://craftcms.com/docs/3.x/dev/filters.html#group" rel="nofollow noreferrer"><code>group</code> Twig filter in Craft</a>. You can group the orders by customer and then output them with two for-loops.</p>
<p>Here's some example code using only the customer ID and order ID:</p>
<pre><code>{% set orders = craft.orders.completed(true).limit(null).orderBy('dateOrdered desc').all() %}
{% set ordersByCustomerId = orders | group(order =&gt; order.customerId) %}

{% for customer, customerOrders in ordersByCustomerId %}
    &lt;h2&gt;{{ customer }}&lt;/h2&gt;

    {% for order in customerOrders %}
        {{ order.id }}&lt;br&gt;
    {% endfor %}

{% endfor %}
</code></pre>

CC BY-SA 4.0
2022-02-04T16:15:58.917Z
2022-02-04T16:15:58.917Z
CraftQuest
<p>Is it possible to print a list of customers but display a customer only once even when he/she ordered multiple times?</p>
<pre><code>    {% set orders = craft.orders.completed(true).limit(null).orderBy('dateOrdered desc').all() %}
    {% for order in orders %}
       &lt;li&gt;
         {{ order.billingAddress.firstName}}
         {{ order.billingAddress.lastName}}
       &lt;/li&gt;
    {% endfor %}
</code></pre>
<p>This works but it prints the names for each order. I want to print the customer only once.</p>

<plugin-craftcommerce>
Commerce show customer only once even with multiple orders
# Commerce show customer only once even with multiple orders

(Asked by: username_0 on 2022-02-04)

Is it possible to print a list of customers but display a customer only once even when he/she ordered multiple times?
    {% set orders = craft.orders.completed(true).limit(null).orderBy('dateOrdered desc').all() %}
    {% for order in orders %}
       
         {{ order.billingAddress.firstName}}
         {{ order.billingAddress.lastName}}
       
    {% endfor %}

This works but it prints the names for each order. I want to print the customer only once.

**Tags:** plugin-craftcommerce


---

## 1 answers

### Answer by username_1 on 2022-02-04. Score: 2 | Accepted answer

You want to use the group Twig filter in Craft. You can group the orders by customer and then output them with two for-loops.
Here's some example code using only the customer ID and order ID:
{% set orders = craft.orders.completed(true).limit(null).orderBy('dateOrdered desc').all() %}
{% set ordersByCustomerId = orders | group(order => order.customerId) %}

{% for customer, customerOrders in ordersByCustomerId %}
    {{ customer }}

    {% for order in customerOrders %}
        {{ order.id }}
    {% endfor %}

{% endfor %}


---

