<p>After some further trial and error I have made this work like so.
However, it feels like there is or should be a simpler method....what am I missing or what is a better approach?</p>
<pre><code>
{% set selectedVehicle = craft.entries
  .section('vehicles')
  .id(vehicle)
  .with( [
    'vehicleBrand',
    'oemFixings',
    'afterMarketFixings'
  ])
  .one() %}

{% set variantsQuery = selectedVehicle.relatedVariants %}

{% set variantsByIDS = [] %}

{% for variant in variantsQuery %}
  {% set variantsByIDS = variantsByIDS|push(variant.id) %}
{% endfor %}

{% set wheelSpacers = craft.products
  .type('wheelSpacers')
  .hasVariant(variantsQuery)
  .orderBy('title ASC')
  .all() %}


Now Show the products and then loop only the variants that are related to the vehicle.

{% for product in wheelSpacers %}
  {% set productVars = product.variants|filter(purchasable =&gt; purchasable.id in variantsByIDS) %}
  &lt;div class=&quot;mb-6&quot;&gt;
    &lt;h2&gt;{{ product.title }}&lt;/h2&gt;
    &lt;div class=&quot;mb-3&quot;&gt;
      &lt;p&gt;{{ product.id }}&lt;/p&gt;
      &lt;p&gt;This product has &lt;strong class=&quot;text-sea-500&quot;&gt;{{ product.variants|length }} options&lt;/strong&gt; in total&lt;/p&gt;
      &lt;p&gt;Only &lt;strong class=&quot;text-sea-500&quot;&gt;{{ productVars|length }} options&lt;/strong&gt; are related&lt;/p&gt;
    &lt;/div&gt;
    
    
    {% for purchasable in productVars %}
      {{ purchasable.title }}
    {% endfor %}
  &lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2021-05-10T05:10:24.993Z
Well one simple thing you can do.... `{% set variantsByIDS = selectedVehicle.relatedVariants.ids() %}` will give you the ids without the need for that loop you're using...indeed, usually with these sorts of things I try and do all the logic just getting/filtering ids, then grab those things by id at the end....tends to make it all a lot simpler and more performant..
Jeremy Daalder
CC BY-SA 4.0
2021-05-07T15:56:08.980Z
2021-05-07T16:04:38.610Z
2021-05-07T16:04:38.610Z
Terry Upton
<p>Struggling with this one. If anyone can help me...?</p>
<p>I have a section of entries called Vehicles.
In this I have a related Variants field type. This has many different variants that related to each vehicle.</p>
<p>On each vehicle page, I want to loop through the main products that are related by their variants but then loop through and only show the related variants that are link to the vehicle.</p>
<p>As an example.</p>
<p>I have a vehicle that has the following related variants.
Product A - Variant 1<br />
Product A - Variant 2<br />
Product A - Variant 3<br />
(product A also has variant 4, 5 and 6)</p>
<p>Product B - Variant 2<br />
Product B - Variant 3<br />
Product B - Variant 6<br />
(product B also has variant 1, 4 and 5)</p>
<p>On the vehicles page. I want to show products A and products B with a list in each showing the related variants. So as per the example. Product A -  would show Var1, 2 and 3 and Product B - would show Var2, 3 and 6.</p>
<p>Here is some code I have as a starting point:</p>
<pre><code>{% set selectedVehicle = craft.entries
  .section('vehicles')
  .id(vehicle)
  .with( [
    'relatedVariants',
    'vehicleBrand',
    'oemFixings',
    'afterMarketFixings'
  ])
  .one() %}

{% set variantsQuery = selectedVehicle.relatedVariants %}
{% set variants = variantsQuery.all() %}

{% set wheelSpacers = craft.products
  .type('wheelSpacers')
  .hasVariant(variantsQuery)
  .orderBy('title ASC')
  .all() %}
</code></pre>

<templating><plugin-craftcommerce><plugin-craftcommerce3>
Get related variants from entry and then show products for related variants and then loop only the variants that are related in commerce
# Get related variants from entry and then show products for related variants and then loop only the variants that are related in commerce

(Asked by: username_0 on 2021-05-07)

Struggling with this one. If anyone can help me...?
I have a section of entries called Vehicles.
In this I have a related Variants field type. This has many different variants that related to each vehicle.
On each vehicle page, I want to loop through the main products that are related by their variants but then loop through and only show the related variants that are link to the vehicle.
As an example.
I have a vehicle that has the following related variants.
Product A - Variant 1
Product A - Variant 2
Product A - Variant 3
(product A also has variant 4, 5 and 6)
Product B - Variant 2
Product B - Variant 3
Product B - Variant 6
(product B also has variant 1, 4 and 5)
On the vehicles page. I want to show products A and products B with a list in each showing the related variants. So as per the example. Product A -  would show Var1, 2 and 3 and Product B - would show Var2, 3 and 6.
Here is some code I have as a starting point:
{% set selectedVehicle = craft.entries
  .section('vehicles')
  .id(vehicle)
  .with( [
    'relatedVariants',
    'vehicleBrand',
    'oemFixings',
    'afterMarketFixings'
  ])
  .one() %}

{% set variantsQuery = selectedVehicle.relatedVariants %}
{% set variants = variantsQuery.all() %}

{% set wheelSpacers = craft.products
  .type('wheelSpacers')
  .hasVariant(variantsQuery)
  .orderBy('title ASC')
  .all() %}

**Tags:** templating, plugin-craftcommerce, plugin-craftcommerce3

---

## 1 answers

### Answer by username_0 on 2021-05-07. Score: 1

After some further trial and error I have made this work like so.
However, it feels like there is or should be a simpler method....what am I missing or what is a better approach?

{% set selectedVehicle = craft.entries
  .section('vehicles')
  .id(vehicle)
  .with( [
    'vehicleBrand',
    'oemFixings',
    'afterMarketFixings'
  ])
  .one() %}

{% set variantsQuery = selectedVehicle.relatedVariants %}

{% set variantsByIDS = [] %}

{% for variant in variantsQuery %}
  {% set variantsByIDS = variantsByIDS|push(variant.id) %}
{% endfor %}

{% set wheelSpacers = craft.products
  .type('wheelSpacers')
  .hasVariant(variantsQuery)
  .orderBy('title ASC')
  .all() %}


Now Show the products and then loop only the variants that are related to the vehicle.

{% for product in wheelSpacers %}
  {% set productVars = product.variants|filter(purchasable => purchasable.id in variantsByIDS) %}
  
    {{ product.title }}
    
      {{ product.id }}
      This product has {{ product.variants|length }} options in total
      Only {{ productVars|length }} options are related
    
    
    
    {% for purchasable in productVars %}
      {{ purchasable.title }}
    {% endfor %}
  
{% endfor %}

> **Comments:**
> **username_1**: Well one simple thing you can do.... `{% set variantsByIDS = selectedVehicle.relatedVariants.ids() %}` will give you the ids without the need for that loop you're using...indeed, usually with these sorts of things I try and do all the logic just getting/filtering ids, then grab those things by id at the end....tends to make it all a lot simpler and more performant..  

---

