2

I have a front end form that allows users check checkboxes to find entries that have matching field values. I've pieced together a query reading other posts here and my limited knowledge of Craft and Twig.

My entries have multiple dropdown fields that are getting matched (1 match per field) and one checkbox field (1 or more matches per field). My query works when I only have dropdown fields but I'm struggling getting it to work to match the checkbox fields.

Here's my paired down query:

    {% set params = {
    section: 'entriesSection',
    limit: null
    } %}

    {% set relatedParams = ['and'] %}

 {# dropdown field match #}
    {% set brand = craft.request.getParam('brand') %}
       {% if getBrand %}
           {% set params = params|merge({'brand':getBrand}) %}
       {% endif %}

 {# checkbox field match #}
    {% set getLocation = craft.request.getParam('location') %}
    {% if getLocation %}
          {% for eachLocation in getLocation %}
              {% set params = params|merge({'eachLocation'}):getLocation}) %}
          {% endfor %}
    {% endif %}

 {# merge related params and params #}
    {% if relatedParams|length > 1 %}
         {% set params = params|merge({'relatedTo':relatedParams}) %}
    {% endif %}

 {# search #}
    {% set entries = craft.entries(params).search(searchParams|trim) %}

Here's my paired down front-end form:

 <form>

<!-- brand matches dropdown fields-->

    <label>
       <input type="checkbox" name="brand[]" value ="brandX" />
    Brand X
    </label>
    <label>
       <input type="checkbox" name="brand[]" value ="brandY" />
    Brand Y
    </label>

 <!-- location matches checkbox fields -->

    <label>
       <input type="checkbox" name="location[]" value ="locationA" />
    Location A
    </label>

    <label>
       <input type="checkbox" name="location[]" value ="locationB" />
    Location B
    </label>
 </form>
  • For the fields that you want to be 'matched', what kind of field type are they? The relatedTo param only works with certain field types and elements. Check out the documentation for more info! – Aaron Berkowitz Oct 20 '15 at 0:52
  • The field type I am having a problem with is a checkboxes field. The other field type is a dropdown and that works ok with the query in my question. – triplethreat23 Oct 20 '15 at 13:09
  • What kind of element is location? If it isn't a category, entry, asset, or user, it wont work with relatedTo. You might need to use search instead. – Aaron Berkowitz Oct 20 '15 at 13:14
  • "Location" is a field associated with an entry. "Brand" is the same just a different field type and that works with this query/relatedto as far as I can tell. Sorry I am really new to this and don't fully understand. According to the documentation, relatedto does work with a field. – triplethreat23 Oct 20 '15 at 15:48
1

I'm probably late to answer this, but I've recentry encountered same problem and here's the solution I came to:

use params for dropdown fields and search for checkbox fields

So my code looks similar to this:

{% set orderParams = {} %}

{% for key, val in craft.request.query %}
    {% if val is not empty %}
        {% if key=='orderFinance' %}
            {% set orderParams = orderParams|merge({orderFinance:val}) %}
        {% elseif key=='orderStatus' %}
            {% set orderParams = orderParams|merge({orderStatus:val}) %}
        {% endif %}
    {% endif %}
{% endfor %}

{% set flagSearchQuery = [] %}
{% for flag in craft.request.query.orderFlag %}
    {% if flag is not empty %}
        {% set flagSearchQuery = flagSearchQuery|merge({query: 'orderFlag:'~flag}) %}
    {% endif %}
{% endfor %}

{% set orders = craft.commerce.orders(orderParams).search(flagSearchQuery).limit(100) %}
| 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.