1

I am running a craft site on craft 2.6.2997 and latest craft commerce.

I am building a url query that allows the user to add query params to the url with a bit of js. The url structure is as follows as an example:

?types[]=coffeeAndSide&materials[]=lacquer

On each click of the tags below it adds/removes the query items to the url and then I use an ajax GET request to pull the data from the page url to show the results.

One piece of trouble I am having is that in the url above the results should technically show something because there are relations in the coffeeAndSide types query and it should show regardless of materials having this item in it. My query setup is below:

{% set typesQuery = craft.request.getParam('types') %}
{% set materialQuery = craft.request.getParam('materials') %}

{# Start an array of search parameters (relationParam) with 'and' to match ALL categories #}
{% set relationParam = ['or','and'] %}
{% set productTypes = [] %}
{% set allValues = [] %}

{# Find categories that match the slugs in your query string... #}
{% for querySlug in materialQuery %}
  {% set materialCategory = craft.categories.group('materials').slug(querySlug) %}

  {# And merge them into the relationParam array. #}
  {% set relationParam = relationParam|merge({targetElement: materialCategory}) %}
  {% set allValues = allValues|merge({targetElement: materialCategory}) %}
{% endfor %}


{% for queryTypeSlug in typesQuery %}

    {% set type = queryTypeSlug %}

    {% set productTypes = productTypes|merge([type]) %}
    {% set allValues = allValues|merge([type]) %}

{% endfor %}

{% if relationParam|length < 1 %}
  {% set relationParam = '' %}
{% endif %}

{% set products = craft.commerce.products({
  type: productTypes
}).relatedTo(relationParam) %}


{% set queriedProducts = products.find() %}

For some reason it doesn't show anything when really it should.

Is there a way I can get it to work where both types and materials can query on their own and if either of them have a result that should technically show?

Thanks, Mark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.