<p>Managed to solve this.</p>
<p>First, I've added a categories field to the order form.</p>
<p>On the checkout page I loop over all lineItems in the cart and collect the category id's like this.</p>
<pre><code>{% set categoryIds = [] %}

{% for item in cart.lineItems %}
  {% set categories = item.purchasable.product.productGroup.leaves().all()%}
  {% for category in categories %}
    {% if category.id not in categoryIds %}
      {% set categoryIds = categoryIds|merge([category.id]) %}
    {% endif %}
  {% endfor %}
{% endfor %}
</code></pre>
<p>Then, I submit the categories using a hidden input.</p>
<pre><code>{% for catId in categoryIds %}
  &lt;input type=&quot;hidden&quot; name=&quot;fields[productGroup][]&quot; value=&quot;{{ catId }}&quot;&gt;
{% endfor %}
</code></pre>
<p>Now I have categories assigned to an order and can easily filter them using <code>.relatedTo()</code>, like this:</p>
<pre><code>{% set ordersQuery = craft.orders().relatedTo(...) %}
</code></pre>

CC BY-SA 4.0
2020-11-28T14:18:23.637Z
2020-11-28T14:18:23.637Z
MetaMachine
<p>I know how to filter entries or products based on a category field with <code>.relatedTo</code>, but how can I filter completed orders based on a product category field?</p>
<p>In this specific case, I have an overview of completed orders. I would like to select a category and show only those orders that contain products with the selected category.</p>
<p>Any hints in the right direction are greatly appreciated!</p>

<filter><category><order><plugin-craftcommerce3><filtering>
Craft Commerce: Filter completed orders based on a product category field
# Craft Commerce: Filter completed orders based on a product category field

(Asked by: username_0 on 2020-11-14)

I know how to filter entries or products based on a category field with `.relatedTo`, but how can I filter completed orders based on a product category field?

In this specific case, I have an overview of completed orders. I would like to select a category and show only those orders that contain products with the selected category.

Any hints in the right direction are greatly appreciated!

**Tags:** filter, category, order, plugin-craftcommerce3, filtering


---

## 1 answers

### Answer by username_0 on 2020-11-28. Score: 1

Managed to solve this.
First, I've added a categories field to the order form.
On the checkout page I loop over all lineItems in the cart and collect the category id's like this.
{% set categoryIds = [] %}

{% for item in cart.lineItems %}
  {% set categories = item.purchasable.product.productGroup.leaves().all()%}
  {% for category in categories %}
    {% if category.id not in categoryIds %}
      {% set categoryIds = categoryIds|merge([category.id]) %}
    {% endif %}
  {% endfor %}
{% endfor %}

Then, I submit the categories using a hidden input.
{% for catId in categoryIds %}
  
{% endfor %}

Now I have categories assigned to an order and can easily filter them using .relatedTo(), like this:
{% set ordersQuery = craft.orders().relatedTo(...) %}


---

