<p>You'll need to query the <a href="https://craftcommerce.com/docs/purchasables" rel="nofollow noreferrer">Purchasable</a> in order to sort by Type.</p>

<p>Hope this helps. Let me know if you find a better way.</p>

<pre><code>&lt;h2&gt;Products&lt;/h2&gt;
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name == 'Products' %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        &lt;li&gt;{{ product.title }} - ({{ item.qty }})&lt;/li&gt;
    {% endif %}
{% endfor %}


&lt;h2&gt;Services&lt;/h2&gt;
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name == 'Services' %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        &lt;li&gt;{{ product.title }} - ({{ item.qty }})&lt;/li&gt;
    {% endif %}
{% endfor %}


&lt;h2&gt;Others&lt;/h2&gt;
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name not in ['Products','Services'] %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        &lt;li&gt;{{ product.title }} - ({{ item.qty }})&lt;/li&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-04-18T20:30:16.777Z
Is there any way to set the particular type of product from the start when querying line items? e.g.

        `{% set items = order.lineItems.type('services') %}`

Obviously that doesn't work, but something along those lines so that I don't have to loop through everything to find what I need?
Matt Everson
CC BY-SA 3.0
2017-04-18T23:51:17.007Z
I think a better approach here might be to, at the top, loop through the order lineItems once and build arrays of each of the result types in one go.  Then below print out those arrays.  Would reduce the queries and thus exec time, anyway.

(I don't believe you can query by lineItem type currently but I might be wrong)
Jeremy Daalder
CC BY-SA 3.0
2017-04-18T17:14:26.893Z
2017-04-18T17:14:26.893Z
casey
<p>I actually needed to do this today for similar reasons.</p>

<p>Here's a plugin that reduces it to a single loop.</p>

<p><a href="https://github.com/simplethemes/sortcart" rel="nofollow noreferrer">https://github.com/simplethemes/sortcart</a></p>

CC BY-SA 3.0
2017-04-20T00:39:45.670Z
2017-04-20T00:39:45.670Z
casey
<p>I have two product types, 1) Materials and 2) Services.</p>

<p>Is there a way on the order summary page to list out the "materials" line items first, followed by the "services" line items below? I can't seem to figure out how to access the product type from <code>cart.lineItems</code>.</p>

<p>Here is what I'm currently using but it seems a little clunky:</p>

<pre><code>&lt;h2&gt;Products&lt;/h2&gt;

{% for item in cart.lineItems %}
    {% set variant = item.purchasable %}
    {% set product = variant.product %}
    {% if product.type == 'materials' %}
        &lt;li&gt;{{ product.title }} - {{ variant.sku }} ({{ item.qty }})&lt;/li&gt;
    {% endif %}
{% endfor %}

&lt;h2&gt;Services&lt;/h2&gt;

{% for item in cart.lineItems %}
    {% set variant = item.purchasable %}
    {% set product = variant.product %}
    {% if product.type == 'services' %}
        &lt;li&gt;{{ product.title }} - {{ variant.sku }} ({{ item.qty }})&lt;/li&gt;
    {% endif %}
{% endfor %}
</code></pre>

<plugin-craftcommerce>
How to list different product types separately in order summary?
# How to list different product types separately in order summary?

(Asked by: username_0 on 2017-04-18)

I have two product types, 1) Materials and 2) Services.
Is there a way on the order summary page to list out the "materials" line items first, followed by the "services" line items below? I can't seem to figure out how to access the product type from cart.lineItems.
Here is what I'm currently using but it seems a little clunky:
Products

{% for item in cart.lineItems %}
    {% set variant = item.purchasable %}
    {% set product = variant.product %}
    {% if product.type == 'materials' %}
        {{ product.title }} - {{ variant.sku }} ({{ item.qty }})
    {% endif %}
{% endfor %}

Services

{% for item in cart.lineItems %}
    {% set variant = item.purchasable %}
    {% set product = variant.product %}
    {% if product.type == 'services' %}
        {{ product.title }} - {{ variant.sku }} ({{ item.qty }})
    {% endif %}
{% endfor %}

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_1 on 2017-04-18. Score: 4

You'll need to query the Purchasable in order to sort by Type.
Hope this helps. Let me know if you find a better way.
Products
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name == 'Products' %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        {{ product.title }} - ({{ item.qty }})
    {% endif %}
{% endfor %}


Services
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name == 'Services' %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        {{ product.title }} - ({{ item.qty }})
    {% endif %}
{% endfor %}


Others
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name not in ['Products','Services'] %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        {{ product.title }} - ({{ item.qty }})
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Is there any way to set the particular type of product from the start when querying line items? e.g.          `{% set items = order.lineItems.type('services') %}`  Obviously that doesn't work, but something along those lines so that I don't have to loop through everything to find what I need?  
> **username_2**: I think a better approach here might be to, at the top, loop through the order lineItems once and build arrays of each of the result types in one go.  Then below print out those arrays.  Would reduce the queries and thus exec time, anyway.  (I don't believe you can query by lineItem type currently but I might be wrong)  

---

### Answer by username_1 on 2017-04-20. Score: 2

I actually needed to do this today for similar reasons.

Here's a plugin that reduces it to a single loop.

<https://github.com/simplethemes/sortcart>


---

