1

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 %}
       <li>
         {{ order.billingAddress.firstName}}
         {{ order.billingAddress.lastName}}
       </li>
    {% endfor %}

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

1 Answer 1

2

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 %}
    <h2>{{ customer }}</h2>

    {% for order in customerOrders %}
        {{ order.id }}<br>
    {% endfor %}

{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.