How to get products with category filter logic #63
-
Looking for some syntax help with filtering. I'm using the backend api. I have your standard ecommerce filter setup with fieldsets containing checkboxes for each of their children (in some cases, nested according to setup in swell). simple example:
I cannot figure how to products i may be misunderstanding the mongo syntax but i feel like i've tried everything and nothing works. thanks! example attempt - ids correspond to child category ids const queryOptions = {
where: {
'$or': [
{
categories: {
'$in': [ '63ae3c88e8b97000134dcb2e', '63ae3c812fc8080012f82b45' ]
}
},
{ categories: { '$in': [ '63ae665b3caf1b001212cbed' ] } },
{
categories: {
'$in': [ '63ae65b52643e60013de7fd7', '63ae42492fc8080012f82d2c' ]
}
}
],
},
limit: 24
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Ideally we could support the If you're building a filtering UI, then typically you'd want to use In any case, here's a way to make that work using the backend API:
Note the |
Beta Was this translation helpful? Give feedback.
-
hey @ericingram , sorry to dig this one up - my last comment on getting products that have a category which is the child of that selected is a big blocker. At the moment, on the store we have no way of showing all products from nested categories, and it is rare to have the parent explicitly set on products. any help here is much appreciated. example: my current query is:
content area nesting: thanks! |
Beta Was this translation helpful? Give feedback.
-
@jezzzm It looks like there's no simplified way to do the There is a special operator on the product model that does retrieve products from sub-categories of a parent, but it doesn't support your mix of logical operators (note In any case here's how it works:
The above will return products in any of those top or sub-categories. Internally the code does something like this:
We should really improve the |
Beta Was this translation helpful? Give feedback.
@jezzzm It looks like there's no simplified way to do the
$and
/$or
logic you need, but you can fetch the sub-categories and make the query work as you described.There is a special operator on the product model that does retrieve products from sub-categories of a parent, but it doesn't support your mix of logical operators (note
$in
is effectively like$or
in this case).In any case here's how it works:
The above will return products in any of those top or sub-categories. Internally the code does something like this: