In the grand scheme, I am trying to get all products that are not related to specific categories.

Since any product can belong to any given number of categories, I am trying to loop through all categories and then only query against the categories that I want.

This is what I have so far:

{% set excludeCategoryIds = [36, 44] %}
{% set allProducts = craft.commerce.products.type('myType').order('title asc') %}
{% set products = [] %}

{% for category in craft.categories.group('myCategories').relatedTo(allProducts) %}
    {% if category.id not in excludeCategoryIds %}
        {% set entries = craft.entries.relatedTo(category) %}

        {# push entires into products?? #}
        {% set products = products|merge([entries]) %} {# doesn't work #}
    {% endif %}
{% endfor %}

Once all products are found, then I'll loop through them.

{% if products|length %}
{% endif %}

I could also loop through each of the entires as described in the docs but it's starting to feel like this is getting out of control.

Is there a better way to only show products that are not related to specific categories?

Thank you for any suggestions!


This line doesn't return an array of items; it returns an ElementCriteriaModel:

{% set entries = craft.entries.relatedTo(category) %}

That's probably why you're having problems with the |merge.

Changing it to the following should do the trick:

{% set entries = craft.entries.relatedTo(category).find() %}
| improve this answer | |

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.