1

I have a form that is set up to include multiple values with checkboxes. I pass all those values into an array

{% set specsQuery = craft.request.getParam('specs') %}
{% set productSpecsCategory = craft.categories.group('productSpecs').slug(specsQuery) %}

My problem is the code below is seeing those values as OR instead of AND. I'm assuming it's because it's all put on the one targetElement line. The amount of criteria a user can select is essentially infinite (as in the max right now is 5 but more categories can be added in craftcms admin area and are created dynamically on the site as more options.

{% set entries = craft.commerce.products('footwear').relatedTo('and',
      { targetElement: productSpecsCategory, field: 'productSpecs' }
) %}

edit: I have tried

{% set entries = craft.commerce.products('footwear').relatedTo(['and',
  { targetElement: productSpecsCategory, field: 'productSpecs' }
]) %}

edit: This works with 2 criteria, but no more or less (obviously). Is there a way to loop the targetElement?

{% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
  {targetElement: productSpecsCategory[0], field: 'productSpecs'},
  {targetElement: productSpecsCategory[1], field: 'productSpecs'}
]) %}
2

This is untested as I dont have anything set that meets these conditions to try it out in, but I think you can use something like this:

{% set targetElements = ['and'] %}
{% for category in productSpecsCategory %}
    {% set targetElements = targetElements|merge([{ targetElement: category }]) %}
{% endfor %}

{% set entries = craft.commerce.products('footwear').relatedTo(targetElements) %}

Inspired by this answer.

  • Yep, that is exactly what I was looking for. I tested it on multiple criteria and it works just like my switch (only better of course). Thanks! – Darryl Hardin Jul 19 '17 at 15:33
1

I am still waiting on a better solution, but this is how I've solved this so far. My query will likely never go over 10, but for multiple reasons a dynamic solution would be much better if there is an answer out there.

{% set queryLength = productSpecsCategory|length %}
{% switch queryLength %}
    {% case 1 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory, field: 'productSpecs'}
        ]) %}

    {% case 2 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'}
        ]) %}
    {% case 3 %}
        {% set entries = craft.commerce.products('footwear').relatedTo([ 'and',
            {targetElement: productSpecsCategory[0], field: 'productSpecs'},
            {targetElement: productSpecsCategory[1], field: 'productSpecs'},
            {targetElement: productSpecsCategory[2], field: 'productSpecs'}
        ]) %}
    {% default %}
{% endswitch %}

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.