1

I'm trying to sort my commerce orders by 2 variables, one being in the order loop, and the second being within the lineitems. I want to order by the lineitem variable first, and THEN the order variable. Something a bit like this:

Order - Group Name LineItem - Product ID

I want to order by custom product ID asc and then Group Name alphabetically. So it would go a little like this:

ID: 1, GroupName: Aaa
ID: 1, GroupName: Bbb
ID: 2, GroupName: Aab

etc...

The current code I have is this:

{% set orders = craft.orders().limit(null).all() %}
{% set orders = orders|supersort('sortAs', '{{ object.groupName }}') %}

{% for order in orders %}
    {% for item in order.lineItems %}
       {{ item.purchasable.product.customProductId }}
    {% endfor %}
{% endfor %}

Ideally, I'd need to do something along the lines of:

{% set orders = orders|supersort('sortAs', '{{ item.purchasable.product.customProductId }} {{ object.groupName }}') %}

But that obviously doesn't work.

1

Managed to do this by putting it all into an array:

{% set productID = [] %}
{% set orders = craft.orders().limit(null).all() %}

{% for order in orders %}
    {% for item in order.lineItems %}
        {% set productID = productID |merge([ {
            'productId' : item.purchasable.product.customProductId,
            'groupName' : order.groupName
        }]) %}
    {% endfor %}
{% endfor %}

{% set productID = productID | supersort('sortAs', '{{ object.productId }} {{ object.groupName }}') %}

{% for theID in productID %}
    {{ theID.groupName }} - {{ theID.productId }}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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