I'm building a book store which filters entries based on GET variables. For the most part, this is working well just merging in various filters to a params variable and then passing craft.entries(params)
.
The tricky bit is that each book can have an ebook price and a paperback price.
If the books are being filtered to a certain format, then I just do this:
{% if 'ebook' in formats %}
{% set params = params|merge({ 'ebookRrp' : 'and, > '~price_from~', < '~price_to }) %}
{% endif %}
{% if 'pback' in formats %}
{% set params = params|merge({ 'paperbackRrp' : 'and, > '~price_from~', < '~price_to }) %}
{% endif %}
That works well for an individual format, but if no format is selected it becomes too specific.
It seems like I need to do something like the following, but this doesn't seem to have any effect on what is returned:
{% set params = params|merge(['or',
{ 'ebookRrp' : 'and, > '~price_from~', < '~price_to },
{ 'paperbackRrp' : 'and, > '~price_from~', < '~price_to }
]) %}
Any ideas?
--- Update with full code: ---
{# Prep the entries params #}
{% set params = {
section: 'books',
with: [
'bookAuthor',
'booksImage',
]
} %}
{# Prep the relatedTo params #}
{% set relatedToParams = ['and'] %}
{# need to do a bunch of checks here and combine them into one relatedto query #}
{% if craft.request.getParam('author') %}
{% set relatedToParams = relatedToParams|merge([{
targetElement: craft.request.getParam('author'),
field: 'bookAuthor'
}]) %}
{% endif %}
{% if entry.section.handle == 'collections' %}
{% set relatedToParams = relatedToParams|merge([{
sourceElement: entry
}]) %}
{% endif %}
{% if craft.request.getParam('genre') %}
{% set relatedToParams = relatedToParams|merge([{
targetElement: ['and']|merge(craft.request.getParam('genre')),
field: 'genre'
}]) %}
{% endif %}
{# Merge in the relatedTo params if they exist #}
{% if relatedToParams|length > 1 %}
{% set params = params|merge({ 'relatedTo' : relatedToParams }) %}
{% endif %}
{% set maxPrice = 10000 %} {# temp high number before working out highest value #}
{% set price_from = craft.request.getParam('price_from') ?? 0 %}
{% set price_to = craft.request.getParam('price_to') ?? maxPrice %}
{% set formats = craft.request.getParam('format') ?? null %}
{% if 'ebook' in formats %}
{% set params = params|merge({ 'isEbook' : 1 }) %}
{% set params = params|merge({ 'ebookRrp' : 'and, > '~price_from~', < '~price_to }) %}
{% endif %}
{% if 'pback' in formats %}
{% set params = params|merge({ 'isPaperback' : 1 }) %}
{% set params = params|merge({ 'paperbackRrp' : 'and, > '~price_from~', < '~price_to }) %}
{% endif %}
{#
{% if formats is null %}
somehow filter price for both formats as an or?
This doesn't work:
{% set params = params|merge(['or',
{ 'ebookRrp' : 'and, > '~price_from~', < '~price_to },
{ 'paperbackRrp' : 'and, > '~price_from~', < '~price_to }
]) %}
{% endif %}
#}
{# Finally get the relevant books #}
{% set books = craft.entries(params) %}
if formats is null
section that is commented out. – Jammooka Mar 19 '16 at 8:34