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