<p>Try this:</p>
<pre><code>{% set relatedProducts = craft.products().relatedTo(currentCategory) %}
</code></pre>
<p>or if you want specific field</p>
<pre><code>{% set relatedProducts = craft.products().relatedTo({
    targetElement: currentCategory,
    field: 'productCategoryFieldInTheProduct'
}) %}
</code></pre>
<p>and after</p>
<pre><code>{% set variants = craft.variants.hasProduct(relatedProducts) %}
</code></pre>
<p>Where:
<strong>currentCategory</strong> - category query, category element or ID</p>

CC BY-SA 4.0
2022-10-14T06:55:18.540Z
Thanks @RomanAvr I'm gonna try this next week. I will keep you posted.
Masiorama
CC BY-SA 4.0
2022-10-14T22:05:16.140Z
You are welcome.

Just tested on craft & commerce 3.x, worked for me
RomanAvr
CC BY-SA 4.0
2022-10-19T14:38:21.200Z
Just tested on commerce 4 and it works. Sorry for the delay.
Masiorama
CC BY-SA 4.0
2022-10-12T21:03:12.153Z
2022-10-12T21:03:12.153Z
RomanAvr
<p>on my new project I decided to show in the page grid variants rather than products, but I am experiencing difficulties, since I am supposed to filter the record set based on a category relation of the product (which holds the variants) rather then on the variants themselves.</p>
<p>If I had to show products in the grid, this would be sufficient:</p>
<p><code>{% paginate craft.products.relatedTo(currentCategory).limit(12) as pageInfo, pageProducts %}</code></p>
<p>But since I want to output the variants my starting point is:</p>
<p><code>{% paginate craft.variants.relatedTo(...?...).limit(12) as pageInfo, pageProducts %}</code>.</p>
<p>A variant holds a relation named <code>product</code> which I do not know how to use in the query builder.</p>
<p>I tried to play a bit with <code>where</code>, <code>having</code> and so on but no luck.</p>
<p>Any suggestions?</p>

<ecommerce><commerce4>
How to query variants on product relatedTo
# How to query variants on product relatedTo

(Asked by: username_0 on 2022-10-12)

on my new project I decided to show in the page grid variants rather than products, but I am experiencing difficulties, since I am supposed to filter the record set based on a category relation of the product (which holds the variants) rather then on the variants themselves.

If I had to show products in the grid, this would be sufficient:

`{% paginate craft.products.relatedTo(currentCategory).limit(12) as pageInfo, pageProducts %}`

But since I want to output the variants my starting point is:

`{% paginate craft.variants.relatedTo(...?...).limit(12) as pageInfo, pageProducts %}`.

A variant holds a relation named `product` which I do not know how to use in the query builder.

I tried to play a bit with `where`, `having` and so on but no luck.

Any suggestions?

**Tags:** ecommerce, commerce4


---

## 1 answers

### Answer by username_1 on 2022-10-12. Score: 1 | Accepted answer

Try this:
{% set relatedProducts = craft.products().relatedTo(currentCategory) %}

or if you want specific field
{% set relatedProducts = craft.products().relatedTo({
    targetElement: currentCategory,
    field: 'productCategoryFieldInTheProduct'
}) %}

and after
{% set variants = craft.variants.hasProduct(relatedProducts) %}

Where:
currentCategory - category query, category element or ID

> **Comments:**
> **username_0**: Thanks @username_1 I'm gonna try this next week. I will keep you posted.  
> **username_1**: You are welcome.  Just tested on craft & commerce 3.x, worked for me  
> **username_0**: Just tested on commerce 4 and it works. Sorry for the delay.  

---

