2

I have a relatively simple structure for products that I am building an advanced search for. I access the products using craft.entries.section('products'), but these products have fields with options for those fields within them.

So my question is, how do I access these? This is my attempt so far.

        {% set parameters=
            {
                section: 'products',
                limit: '1'
            }
        %}

        {% set query = craft.request.getParam('entry') %}

        {% if query %}
            {% set entries = craft.entries(parameters).relatedTo(query).order('score') %}
        {% else %}
            {% set entries = craft.entries(parameters) %}
        {% endif %}

I know this is incorrect, because I need to look at an object within an object within products and check a field belonging it. For instance, if the product was a phone, it would have something like "mobilityOptions" and under that would be "Cordless", "Desk", and "Wall".

My search form has got those mobility options through a for option in products loop of the section, followed by a for block in option.mobilityOptions.options that then makes checkboxes for each option, but to use it with the ".relatedTo" functionality I don't think it would work.

4

Checkboxes / multiselect fields store to the database in a very query unfriendly way. That's why I would really recommend to use categories, tags, lightswitches instead, if you intend to query against these fields' values.

Here's what you'd have to do to search for entries with selected checkboxes field types' options. You basically have to get all relevant entries first, go through them to check each checkbox option and store the element ID in case the entry is valid. Using this approach you end up with an array of IDs which you can now query against.

{% set entries = craft.entries.section('news') %}

{% set query = craft.request.getQuery() %}

{% if query.mobilityOptions is defined %}
    {% set ids = [] %}

    {% for entry in entries %}
        {% set valid = true %}

        {% for option in query.mobilityOptions %}
            {% if not entry.mobilityOptions.contains(option) %}
                {% set valid = false %}
            {% endif %}
        {% endfor %}

        {% if valid %}
            {% set ids = ids|merge([entry.id]) %}
        {% endif %}
    {% endfor %}

    {% set entries = craft.entries.id(ids) %}
{% endif %}
| improve this answer | |
  • Is there a way to add entities to a category when a field is checked/unchecked via the admin panel? If so, then that would definitely be the best option. If not, then I can't really add categories based on what I am looking for simply because it adds more workload for the person updating the content. I'm going to try the solution you posted to see if it gets what I need, but I have done some filtering based on categories and it was a lot cleaner and intuitive than this. – Nexcapto Jan 13 '16 at 13:00
  • I'm not sure what you mean with "add entities to a category when a field is checked/unchecked"? – carlcs Jan 13 '16 at 13:53
  • When someone is in the admin panel, and say they add a product. When an entry field for mobilityOptions is checked, say they check "Cordless", is there a way to automatically add them to a category called "Cordless Phones" instead of them clicking cordless, as well as adding it to that category? Also thanks for the help and follow-up! I'm still not quite there on the solution though.. – Nexcapto Jan 13 '16 at 14:00
  • "When an entry field for mobilityOptions is checked" – do you mean an Entries field? I was actually suggesting to replace the checkboxes field with an Categories field that you would link up to a category group "Mobility Options". Sorry, but I guess I miss something. – carlcs Jan 13 '16 at 14:13
  • I'm with you. I understand you suggested I use categories instead of the fields. But what I am asking is, because I am working on a live site with a lot of entries, is it possible to sweep through them all, and place them in a category based on that field so that when I do set up the search by category, it would actually find the results. Does that explain it better? I'm looking for a way to add products to categories based on field selections when creating them. They still need those fields for when you are on the products page. – Nexcapto Jan 13 '16 at 14:44
2

Have a look at the Checkbox field plugin.

It provides a field type that looks like the default Checkboxes field, but stores a boolean value to the database (similar to the Lightswitch field), so you can use a custom field parameter in your criteria model.

{% set entries = craft.entries.myCheckbox(1).myOtherCheckbox(0) %}
| improve this answer | |
  • I marked your other answer as it got me what I needed. Thanks! – Nexcapto Jan 18 '16 at 15:26

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.