I want to set up a product filter system based on variants' tags. I encounter two difficulties.

1. Outputting all variants' tags from a group of products.

<!-- Relation does not work -->
{% set products = craft.commerce.products.type('Custom').find() %}
{% set variants = craft.commerce.variants.relatedTo(products) %} 

{% for pattern in craft.tags.group('patternSwatch').relatedTo(variants) %}
    <a href="?pattern={{ pattern.name | url_encode }}">{{ pattern.name }}</a>
{% endfor %}

<!-- This works but returns tags from ALL products' variants -->
{% for pattern in craft.tags.group('patternSwatch').relatedTo(craft.commerce.variants) %}
    <a href="?pattern={{ pattern.name | url_encode }}">{{ pattern.name }}</a>
{% endfor %}

2. Filter product based on the multiple variants' tags

<!-- This works, but what about filtering by multiple tags? -->
{% set pattern = craft.request.getParam('pattern') %}
{% set tag = craft.tags.group('patternSwatch').title(pattern).first() %}
{% set variants = craft.commerce.variants.relatedTo(tag) %}

{% for v in variants %}
    {{ v.product.slug }}
{% endfor %}

1 Answer 1


Personally I would use categories for a 'facetted search' filter you describe, as categories are less likely to get out of control. e.g. If tags are seldom re-used then a search for a tag may only produce a single result.

This plugin may help you: https://github.com/iainurquhart/FacetedNav_CraftPlugin

If you're going to use tags then I suggest making use of Craft's search functionality as this will allow you to submit the tag as a search term and also display any entries which contain the word/tag in ANY field of the entries within Craft.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.