I have troubles understanding the nested twig query syntax. It works fine in PHP code but somehow when using twig I never seem to get it right.
This is my raw, dirty query which I want to clean up a bit:
{% set tempQuery = craft.products()
.type('product')
.where('(field_onlyShowMainProduct_njwytbyx = 0 OR field_onlyShowMainProduct_njwytbyx IS
NULL) OR (field_onlyShowMainProduct_njwytbyx = 1 AND
field_f_product_isMainProduct_ftgnylbr = 1)')
%}
I managed to get it cleaner in PHP:
Product::find()
->type('product')
->where([
'or',
['field_onlyShowMainProduct_njwytbyx' => [0, null]],
['and',
['field_onlyShowMainProduct_njwytbyx' => 1],
['field_isMainProduct_ftgnylbr' => 1]
]
])
But in twig I can't get it to work. I just get error after error. Feels like i'm missing some basic understanding of nesting and twig formatting arrays. When to use "," or ":", "{" or "[". The examples in the Craft docs are also very simple queries. Any ideas? Suggestions? Fixes?
{% set tempQuery = craft.products()
.type('product')
.where({
or: [
{ 'field_onlyShowMainProduct_njwytbyx': [0, null] },
{ and: [
{ 'field_onlyShowMainProduct_njwytbyx': 1 },
{ 'field__isMainProduct_ftgnylbr': 1 }
]}
]
})
%}