2

I've got categories setup like this (Parent & Children):

Type
    Tops
    Dresses
    Pants

Colour
    Black
    Red
    Yellow

When I use this in a category filter and choose Dresses and Black, it works fine in terms that it will return entries that are in Dresses and entries that are in Black.

But how can I edit my code so that it returns entries that are ONLY in Dresses and Black (Black Dresses only!)

Below is what I have so far...

{% set productSegmentCategory = craft.categories.slug(craft.request.lastSegment()).first() %}
{% set productCategoriesArray = ['and'] %}

{% set productValues = {
    keywords: craft.request.getParam('keywords'),
    category: craft.request.getParam('category') ? craft.categories.slug(craft.request.getParam('category')) : null,
    order: craft.request.getParam('order') ? craft.request.getParam('order') : 'title asc',
    limit: craft.request.getParam('limit') ? craft.request.getParam('limit') : entryLimit,
} %}

{% set productParameters = {
    search: productValues.keywords,
    order: productValues.order,
    limit: null,
}%}

{% if productSegmentCategory|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productSegmentCategory, field: 'productCategories' }]) %}
{% endif %}

{% if productValues.category|length %}
    {% set productCategoriesArray = productCategoriesArray|merge([{ targetElement: productValues.category, field: 'productCategories' }]) %}
{% endif %}

{% if productCategoriesArray|length > 1 %}
  {% set productParameters = productParameters|merge({ relatedTo: productCategoriesArray }) %}
{% endif %}

{% set productEntries = craft.commerce.products(productParameters) %}

{% if productSegmentCategory|length %}
    {% set entry = productSegmentCategory %}
{% endif %}
2

You can limit to only products that have multiple related categories like so:

{% set dressesCat = craft.categories...first() %}
{% set blackCat = craft.categories...first() %}

{% set products = craft.commerce.products({
    relatedTo: {
        targetElement: ['and', dressesCat, blackCat],
        field: 'productCategories'
    }
} %}

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.