<p>The "variant" method can receive multiple ids using the following: <code>craft.variants().id([1,2,3])</code>.</p>

<p>Following that, you can easily populate an array while looping through your (specific) products, and then use that array to fetch the variants.</p>

<p><a href="https://docs.craftcms.com/commerce/v2/dev/element-queries/variant-queries.html#id" rel="nofollow noreferrer">Official documentation</a></p>

CC BY-SA 4.0
2019-01-15T15:46:50.137Z
This helped me understand it. Thanks! See my answer for the final markup. :)
Jay
CC BY-SA 4.0
2019-01-14T20:24:15.500Z
2019-01-16T16:59:55.117Z
2019-01-16T16:59:55.117Z
Maxime Lafrenière
<p>I need to get a list of variants from multiple products, but in a way that allows us to show the results in Craft's handy <code>{% paginate %}</code> tags.</p>

<p>So far we have this less-than-appealing markup:</p>

<pre><code>{% set firstProduct  = craft.products.relatedTo(category).one() %}
{% set results = craft.variants({product: firstProduct}) %}

{% set products  = craft.products.relatedTo(category).all() %}

{% for product in products %}
    {% set results = results|merge(craft.variants({product: product})) %}
{% endfor %}
</code></pre>

<p>However, because we're using merge it's turning our <code>ElementCriteriaModel</code> into an <code>Array</code>. Is there a way to use the <code>{{ craft.variants({product: product}) }}</code> tag and pass it multiple products?</p>

<p>(Craft3 and Commerce2)</p>

<p>Thanks</p>

<p>EDIT: Final markup that ended up working:</p>

<pre><code>{# All products within this category #}
{% set products = craft.products.relatedTo(category).all() %}

{# Empty variant id array #}
{% set variantIds = [] %}

{# Populating variant id array #}
{% for product in products %}
    {% set variantIds = variantIds|merge(craft.variants({product: product}).ids()) %}
{% endfor %}

{# Get all variants from the variant id array #}
{% set results = craft.variants.id(variantIds).orderBy('title asc') %}
</code></pre>

<craft3><plugin-craftcommerce><elementcriteriamodel><array><plugin-craftcommerce2>
Get all variants from multiple products
# Get all variants from multiple products

(Asked by: username_0 on 2019-01-14)

I need to get a list of variants from multiple products, but in a way that allows us to show the results in Craft's handy {% paginate %} tags.
So far we have this less-than-appealing markup:
{% set firstProduct  = craft.products.relatedTo(category).one() %}
{% set results = craft.variants({product: firstProduct}) %}

{% set products  = craft.products.relatedTo(category).all() %}

{% for product in products %}
    {% set results = results|merge(craft.variants({product: product})) %}
{% endfor %}

However, because we're using merge it's turning our ElementCriteriaModel into an Array. Is there a way to use the {{ craft.variants({product: product}) }} tag and pass it multiple products?
(Craft3 and Commerce2)
Thanks
EDIT: Final markup that ended up working:
{# All products within this category #}
{% set products = craft.products.relatedTo(category).all() %}

{# Empty variant id array #}
{% set variantIds = [] %}

{# Populating variant id array #}
{% for product in products %}
    {% set variantIds = variantIds|merge(craft.variants({product: product}).ids()) %}
{% endfor %}

{# Get all variants from the variant id array #}
{% set results = craft.variants.id(variantIds).orderBy('title asc') %}

**Tags:** craft3, plugin-craftcommerce, elementcriteriamodel, array, plugin-craftcommerce2


---

## 1 answers

### Answer by username_1 on 2019-01-14. Score: 2 | Accepted answer

The "variant" method can receive multiple ids using the following: `craft.variants().id([1,2,3])`.

Following that, you can easily populate an array while looping through your (specific) products, and then use that array to fetch the variants.

[Official documentation](https://docs.craftcms.com/commerce/v2/dev/element-queries/variant-queries.html#id)

> **Comments:**
> **username_0**: This helped me understand it. Thanks! See my answer for the final markup. :)  

---

