New answers tagged

3

You could use filter to get only the variant that matches the wheelSpacer variable. This will return an array, so you can use first to get the first matching variant (since presumably product.variants won't contain duplicate variants). This has the added benefit of getting you the matching variant directly, instead of it's ID: {% set variant = product....


2

Using the |map filter would work nicely here: {% set variantIds = product.variants|map(variant => variant.id) %} {% if wheelSpacer in variantIds %} ... {% endif %}


2

If you don't want to build a custom plugin, this would be a good time to use Twig's reduce filter, which takes a set of values and iterates over them to output a single value based on supplied logic. Using Andy's answer as the basis might look like this (untested): {% set currentYear = now|date('Y') %} {% set loyaltyScore = currentUser.years.all()|reduce( ...


1

Pseudo-code: Fetch all category values, push the year string to an array. Sort that array in descending order by value. Initialise a counter, loop over the array, cast each value as an integer: -- if first array value is same as the current year, increase a counter, otherwise break. -- if loop year equals previous loop year minus 1, increase a counter ...


Top 50 recent answers are included