2

I'm trying to figure out a way from Twig to get a distinct list of products purchased by a user. Help?

1 Answer 1

5

Something like this should work:

{# Grab the currently logged in users past orders %}
{% set pastOrders = craft.commerce.orders.customer(craft.commerce.customer).find() %}

{% set uniqueSKUs = [] %}

{# loop through the orders #}
{% for order in pastOrders %}

    {# loop through the line items #}
    {% for item in order.lineItems %}

        {# did we find a unique one? #}
        {% if item.purchasable.sku not in uniqueSKUs %}

            {# add it to the list #}
            {% set uniqueSKUs = uniqueSKUs|merge([item.purchasable.sku]) %}

        {% endif %}
    {% endfor %}
{% endfor %}

<h1>Unique SKUs for customer ID {{ craft.commerce.customer.id }}:</h1>

<ul>
    {% for sku in uniqueSKUs %}
        <li>{{ sku }}</li>
    {% endfor %}
</ul>

Your Answer

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

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