1

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:

<h2>Products</h2>

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

<h2>Services</h2>

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

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.

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


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


<h2>Others</h2>
{% for item in order.lineItems %}
    {% if item.purchasable.product.getType().name not in ['Products','Services'] %}
        {% set variant = item.purchasable %}
        {% set product = variant.product %}
        <li>{{ product.title }} - ({{ item.qty }})</li>
    {% endif %}
{% endfor %}
| improve this answer | |
  • 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 Apr 18 '17 at 20:30
  • 1
    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 Apr 18 '17 at 23:51
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

| improve this answer | |

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.