Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Include subcategories when filtering by product categories (#17936)

Iga Karbowiak1088948818ee
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2a0b5e16fd..b033b48eb5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -82,6 +82,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Extend `AttributeEntityType` with `CATEGORY` and `COLLECTION`. You can now assign category and collection as a attribute reference.
- Attribute values now expose the `referencedObject`, allowing for easier access to the linked entity.
- You can now filter and search attribute choices using the new `where` and `search` fields on the `attribute.choices` query.
+- Filtering products by `category` now also includes subcategories. The filter will return products that belong to the specified categories as well as their subcategories.
- Deprecated `Transaction.gatewayResponse` field. Please migrate to Transaction API and Apps.
- Extend the `Attribute` type with a `values` field, allowing you to retrieve all values assigned to a specific attribute.
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 6d33b9b093..d2e3d5ffeb 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -1107,6 +1107,22 @@ def where_filter_updated_at_range(qs, _, value):
return filter_where_range_field_with_conditions(qs, "updated_at", value)
+def where_filter_by_categories(qs, value):
+ """Filter products by categories and subcategories of provided categories."""
+ if not value:
+ return qs.none()
+ eq = value.get("eq")
+ one_of = value.get("one_of")
+ pks = None
+ if eq and isinstance(eq, str):
+ _, pks = resolve_global_ids_to_primary_keys([eq], "Category", True)
+ if one_of:
+ _, pks = resolve_global_ids_to_primary_keys(one_of, "Category", True)
+ if pks:
+ return filter_products_by_categories(qs, pks)
+ return qs.none()
+
+
class ProductWhere(MetadataWhereFilterBase):
ids = GlobalIDMultipleChoiceWhereFilter(method=filter_by_ids("Product"))
name = OperationObjectTypeWhereFilter(
@@ -1215,7 +1231,7 @@ class ProductWhere(MetadataWhereFilterBase):
@staticmethod
def filter_category(qs, _, value):
- return filter_where_by_id_field(qs, "category", value, "Category")
+ return where_filter_by_categories(qs, value)
@staticmethod
def filter_collection(qs, _, value):
diff --git a/saleor/graphql/product/tests/queries/test_products_query_with_where.py b/saleor/graphql/product/tests/queries/test_products_query_with_where.py
index b0f51d3a40..78d52344d1 100644
--- a/saleor/graphql/product/tests/queries/test_products_query_with_where.py
+++ b/saleor/graphql/product/tests/queries/test_products_query_with_where.py
@@ -247,6 +247,44 @@ def test_product_filter_by_categories(
}
+def test_product_filter_by_subcategories(
+ api_client, product_list, channel_USD, category_list
+):
+ # given
+ subcategory_1 = category_list[0]
+ subcategory_2 = category_list[1]
+ parent_category = category_list[2]
+
+ subcategory_1.parent = parent_category
+ subcategory_2.parent = parent_category
+ subcategory_1.save()
+ subcategory_2.save()
+
+ product_list[0].category = subcategory_1
+ product_list[1].category = subcategory_2
+ Product.objects.bulk_update(product_list, ["category"])
+
+ category_id = graphene.Node.to_global_id("Category", parent_category.pk)
+
+ variables = {
+ "channel": channel_USD.slug,
+ "where": {"category": {"eq": category_id}},
+ }
+
+ # when
+ response = api_client.post_graphql(PRODUCTS_WHERE_QUERY, variables)
+
+ # then
+ data = get_graphql_content(response)
+ products = data["data"]["products"]["edges"]
+ assert len(products) == 2
+ returned_slugs = {node["node"]["slug"] for node in products}
+ assert returned_slugs == {
+ product_list[0].slug,
+ product_list[1].slug,
+ }
+
+
def test_product_filter_by_category(
api_client, product_list, channel_USD, category_list
):