3

I have a suitability category field in my product type that allows the content writer to select which categories this product is associated with.

But my issue is that on the product page I need to show a full list of these 'suitability' categories with a "yes" or "no" next to them indicating which categories are appropriate.

So I am getting the 'suitability' category group:

{% set suitability = craft.categories.group('suitability').all() %}

Then I am simply looping over this list, but I'm unsure how to format my if statement to dictate if this category needs a "yes" or a "no". I know it's probably something to do with the .relatedTo() parameter but I'm not quite sure on the formatting.

{% for category in suitability %}
    <dt class="data-list__item data-list__item--title">{{ category.title }}</dt>
    <dd class="data-list__item data-list__item--value">
        {% if product.relatedTo(category) %}Yes{% else %}No{% endif %}
    </dd>
{% endfor %}

Thanks!

3

Indigo Viking's code will achieve what you want, but if you want a simpler solution (possibly a bit more performant since there's no second for loop), you could do this instead:

{% set suitability = craft.categories.group('suitability').all() %}

{% for category in suitability %}
    <dt class="data-list__item data-list__item--title">
        {{ category.title }}
    </dt>
    <dd class="data-list__item data-list__item--value">
        {% if category.id in product.suitability.ids() %}
            Yes
        {% else %}
            No
        {% endif %}
    </dd>
{% endfor %}

The way this works is product.suitability.ids() returns an array of the element ids of the categories selected in your suitability field. Then you just check if the current category's id is in that array.

Fast, simple, and easy!

  • Lovely stuff, I knew there was a really simple way of comparing items. Cheers, Jalen. – Jay Dec 19 '18 at 16:18
2
{% set suitability = craft.categories.group('suitability').all() %}

{% for category in suitability %}
    <dt class="data-list__item data-list__item--title">
        {{ category.title }}
    </dt>
    <dd class="data-list__item data-list__item--value">
        {% for selectedCategory in product.suitability.all() %}
            {% if selectedCategory.slug == category.slug %}
                Yes
            {% else %}
                No
            {% endif %}
        {% endfor %}
    </dd>
{% endfor %}

You'll want to loop over the list of selected categories in your product's category field (product.suitability) and then check the if the slug of each selected category is the same as the current slug in your main for loop.

  • This is kind of working. It's only saying "yes" by the first suitability category I've selected though. – Jay Dec 19 '18 at 11:52
  • Missed the loop for the field itself. updating answer. – Indigo Viking Dec 19 '18 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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