1

I have the following code that works with Sprig. But with the new andRelatedTo param, I am wondering if this could be simplified? However, I am a little unsure if/where to start on this and how the new andRelatedTo would replace existing code.

Can anyone steer me in the right direction?

{% set defaultLimit = 35 %}
{% set search = search ?? '' %}
{% set sort = sort ?? 'title ASC' %}
{% set limit = limit ?? craft.app.request.queryParam('limit') ?? defaultLimit %}
{% set page = page ?? 1 %}
{% set category = craft.categories.id(_categoryId).one() %}

{# {% set alllowedEntryTypeHandles = ['sponsored', 'free'] %} #}
{% set limitOptions = [2, 5, 10, 25, 35, 50, 100, 'All'] %}
{% set limit = limit in limitOptions ? limit : defaultLimit %}

{% set params = {
  limit:limit,
  page: page,
} %}

{% set relatedParams = ['and'] %}
{% set relatedParams = relatedParams|merge([]) %}
{% set filterIds = '' %}

{% set pcdQueryString = null %}
{% set cbQueryString = null %}
{% set finishQueryString = null %}

{% if craft.app.request.queryParam('pcd') %}
  {% set pcdQueryString = craft.app.request.queryParam('pcd') | join(',') %}
  {% set filterIds = filterIds ~ ',' ~ pcdQueryString|join(',') %}
  {% set relatedParams = relatedParams|merge([pcdQueryString]) %}
  {% set params = params|merge({pcd:pcdQueryString}) %}
{% endif %}

{% if craft.app.request.queryParam('cb') %}
  {% set cbQueryString = craft.app.request.queryParam('cb') | join(',') %}
  {% set filterIds = filterIds ~ ',' ~ cbQueryString|join(',') %}
  {% set relatedParams = relatedParams|merge([cbQueryString]) %}
  {% set params = params|merge({cb:cbQueryString}) %}
{% endif %}

{% if craft.app.request.queryParam('finish') %}
  {% set finishQueryString = craft.app.request.queryParam('finish') | join(',') %}
  {% set filterIds = filterIds ~ ',' ~ finishQueryString|join(',') %}
  {% set relatedParams = relatedParams|merge([finishQueryString]) %}
  {% set params = params|merge({finish:finishQueryString}) %}
{% endif %}

{# SAERCH #}
{% if craft.app.request.queryParam('search') %}
  {% set params = params|merge({search:search}) %}
{% endif %}

 {% set filterTags = craft.entries
   .id(filterIds)
   .orderBy('title ASC')
   .all() %}

{% set allProductsQuery = craft.products
  .relatedTo(category)
  .all() %}

 {% set productsQuery = craft.products
   .search(search)
   .with( [
     'department',
     'productImage',
     'finish',
     'pcd',
     'variants',
     'thread',
     'seat',
     'hexHead'
   ])
   .orderBy(sort)
   .limit(limit)
   .relatedTo(relatedParams) %}

{% do sprig.pushUrl('?' ~ params|url_encode) %}
3

That's a lot of code to post in a question!

The basic gist is that instead of merging new related params with relatedParams|merge and then applying to the query, you can call andRelatedTo(newParams) directly on the query.

{% set productsQuery = craft.products %}
{% set someEntryId = craft.app.request.queryParam('someEntryId') %}

{# BEFORE #}
{% if someEntryId %}
    {% set relatedParams = relatedParams|merge([someEntryId]) %}
{% endif %}
{% set productsQuery = productsQuery.relatedTo(relatedParams) %}

{# AFTER #}
{% if someEntryId %}
    {% do productsQuery.andRelatedTo(someEntryId) %}
{% endif %}

I explain this in more detail in the Sprig Filters video series.

1
  • Can you mark this as the correct answer, assuming you are satisfied with it, so that this question appears resolved?
    – Ben Croker
    Aug 22 '21 at 7:29

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.