3

I am attempting to create a relationship between products. I have a section named "product" which is a Structure. Each "product" entry has a category field called "species" and a category field called "category".

My "species" category is a nested category. For example:

Dogs
  |-- Dachshund
  |-- Chiwawa
Cats
  |-- Long Hair
  |-- Short Hair

My "category" category is a single list. For example:

Yellow
Green
Blue

I have a form that I am trying to filter back my entries based on which "species" and/or "category" is chosen from the drop-down menu(s).

I have the ajax part down - no problem. I am sending a request to a twig .json template, and I am always getting into my "No Results" condition.

Here is the twig syntax I'm using. I have to be close (hopefully)...

{% set species = craft.request.getParam('species') %} // returns "Dachshund"
{% set category = craft.request.getParam('category')  // returns "Yellow"

{% set products = craft.entries.section('product').relatedTo('or',
{ sourceElement: category, field: 'category' },
{ targetElement: species, field: 'species' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                    {
                        "name": "{{ product.title }}"
                    } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}

I have also been trying to follow this thread in SO to search for entries.

I know I need a conditional (this or that) since a user could choose a "species" but not a "category" so this article from the docs is where I need to be.

I have gotten results back, but then run into an error that a class can't be converted to a string. I assume that's because I don't have the syntax correct to dig down into the category group correctly.

Within my "product" entry, the "species" category field looks like this:

Dogs
  |--Dachshund

I am also 100% confident the ElementAPI would be extremely helpful, I just haven't figured out how to incorporate it.

EDIT

Here is my updated products.json file that I am submitting the form to. I still fall into the NO_RESULTS condition.

{% if craft.request.isAjax %}
{% set species = craft.request.getParam('species').first() %}
{% set category = craft.request.getParam('category').first() %}

{% set foo = craft.entries.section('product').species(species) %}
{% set bar = craft.entries.section('product').category(category) %}

{% set products = craft.entries.section('product').relatedTo('or',
    { sourceElement: foo, field: 'species' },
    { targetElement: bar, field: 'category' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                  {
                    "name": "{{ product.title }}"
                  } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}
{% endif %}
Damon
  • 4,706
  • 1
  • 20
  • 36

1 Answers1

2

I finally figured everything out, It took a long time to figure out what I actually needed to search for to find the answers I needed.

I put a comment at the bottom of this thread that was also of tremendous help. Passing multiple arguments to find related entries via categories.

Damon
  • 4,706
  • 1
  • 20
  • 36