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') %}