Graphql OR condition not working
Preconditions (*)
Clean Magento 2.3
- Simple core graphql request , product filtering
- Json used for request
{ products( filter: { or: { price: {gt:"1"} name: {like: "%Bag"} } } pageSize: 25 sort: { price: DESC } ) { total_count items { name sku price { regularPrice { amount { value currency } } } } page_info { page_size current_page } } }
Expected result (*)
1.This should return all products where price is greater then 1, or name ends with Bag (using OR condition)
Actual result (*)
- This does not make an OR condition, it is actually an AND contdition
The filtering works properly if instead of the price we use a different attribute, but with price AND logic is taken in consideration. Is there a limitation with price filtering?
I'm also interested in this topic
have you tried setting the or condition in an array, not object ?
Reason - Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor::addFilterGroupToCollection() function check getCustomFilterForField which list inject from app/code/Magento/CatalogGraphQl/etc/graphql/di.xml -> customFilters(line: 67) virtual type.
when use price clause in the filter then compare with custom filter list('price') and skip to add to or clause. instead of that its add to the where clause in function Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter::apply().

Hi @dominicfernando ,
Is this a bug, that will be fixed, or price cannot be used with OR condition ?
Thanks,
Hi @ksz2013,
In my point of view, this is a bug. and also we can't use price, min_price, max_price, and category_id with OR condition.
This functionality needs to refactor.
Eg. When the filter has the price, min_price, max_price, and category_id then
#1 - Do the needful like sql joins in Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter::apply()) but not add to sql to where clause
#2: Instead of that add to the Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor::addFilterGroupToCollection() => addfieldToFilter()
Hi, @ksz2013 .
It is a bug indeed, but I think there are no possible stable solutions for that without architectural changes.
Any fields which are not attributes (price, min_price and max_price are stored in price index table) cannot be applied using OR condition.
Partially this problem is related to https://github.com/magento/magento2/issues/4060
This issue needs someone who has enough time and skills to suggest new architecture.
@mslabko please provide more information about plans related to OR condition
Thanks
Hi @ksz2013 ,
We plan to eliminate support of "OR" condition starting from 2.4 for GraphQL (https://github.com/magento-performance/architecture/blob/graphql/design-documents/graph-ql/storefront-api.md#filtration)
Moreover, currently, Magento does not have scenarios on Store Front where "OR" condition is used. Note, we still will support "OR" condition for the same attribute, e.g. color IN ("red", "blue")
Could you please describe a business case where you need support for "OR" condition?
Hi @mslabko,
I can do it instead of @ksz2013
Sometimes I need to have OR condition in cases like baby carriage
color IN ('blue', 'grey') # it is a boy so both colors a are fit
BUT also I need raincoat and mosquito net (both of equipment)
Thanks
@naydav , the first case is covered. For the second one, you still can do 2 requests. @maghamed what do you think regarding this case?