Skip to content

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

Allow filtering orders by lines count (#17806)

Iga Karbowiakda11036b099a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2b111a4182..afbaf7a2e5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Filter by associated invoice creation date.
- Filter by fulfillment existence.
- Filter by associated fulfillment status.
+ - Filter by number of lines in the order.
- Extend the `Page` type with an `attribute` field. Adds support for querying a specific attribute on a page by `slug`, returning the matching attribute and its assigned values, or null if no match is found.
### Webhooks
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.py
index 4234f60e6e..ad0e510a13 100644
--- a/saleor/graphql/order/filters.py
+++ b/saleor/graphql/order/filters.py
@@ -499,6 +499,11 @@ class OrderWhere(WhereFilterSet):
method="filter_fulfillments",
help_text="Filter by fulfillment data associated with the order.",
)
+ lines_count = OperationObjectTypeWhereFilter(
+ input_class=IntFilterInput,
+ method="filter_lines_count",
+ help_text="Filter by number of lines in the order.",
+ )
@staticmethod
def filter_number(qs, _, value):
@@ -604,6 +609,10 @@ class OrderWhere(WhereFilterSet):
return qs.filter(Exists(fulfillments.filter(order_id=OuterRef("id"))))
return qs.none()
+ @staticmethod
+ def filter_lines_count(qs, _, value):
+ return filter_where_by_numeric_field(qs, "lines_count", value)
+
class OrderWhereInput(WhereInputObjectType):
class Meta:
diff --git a/saleor/graphql/order/tests/queries/test_order_with_where.py b/saleor/graphql/order/tests/queries/test_order_with_where.py
index bb3a9d1095..46b206631b 100644
--- a/saleor/graphql/order/tests/queries/test_order_with_where.py
+++ b/saleor/graphql/order/tests/queries/test_order_with_where.py
@@ -1576,6 +1576,50 @@ def test_orders_filter_by_fulfillment_status(
assert numbers == {str(order_list[index].number) for index in indexes}
+ ("where", "indexes"),
+ [
+ ({"range": {"gte": 2, "lte": 4}}, [1, 2]),
+ ({"range": {"gte": 3}}, [2]),
+ ({"range": {"lte": 2}}, [0, 1]),
+ ({"eq": 2}, [1]),
+ ({"oneOf": [1, 3]}, [0, 2]),
+ ({"eq": 99}, []),
+ ({}, []),
+ ({"range": {"gte": None}}, []),
+ ({"range": {"lte": None}}, []),
+ ({"eq": None}, []),
+ ({"oneOf": []}, []),
+ (None, []),
+ ],
+)
+def test_orders_filter_by_lines_count(
+ where,
+ indexes,
+ order_list,
+ staff_api_client,
+ permission_group_manage_orders,
+):
+ # given
+ order_list[0].lines_count = 1
+ order_list[1].lines_count = 2
+ order_list[2].lines_count = 3
+ Order.objects.bulk_update(order_list, ["lines_count"])
+
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+ variables = {"where": {"linesCount": where}}
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_WHERE_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["orders"]["edges"]
+ assert len(orders) == len(indexes)
+ numbers = {node["node"]["number"] for node in orders}
+ assert numbers == {str(order_list[index].number) for index in indexes}
+
+
def test_order_query_with_filter_and_where(
staff_api_client,
permission_group_manage_orders,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index aaae35ec4e..223718624f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -12880,6 +12880,9 @@ input OrderWhereInput @doc(category: "Orders") {
"""Filter by fulfillment data associated with the order."""
fulfillments: FulfillmentFilterInput
+ """Filter by number of lines in the order."""
+ linesCount: IntFilterInput
+
"""List of conditions that must be met."""
AND: [OrderWhereInput!]
diff --git a/saleor/graphql/utils/filters.py b/saleor/graphql/utils/filters.py
index 0d850caa41..c5f5df1dfb 100644
--- a/saleor/graphql/utils/filters.py
+++ b/saleor/graphql/utils/filters.py
@@ -132,6 +132,9 @@ def filter_where_by_numeric_field(
field: str,
value: dict[str, Number | list[Number] | dict[str, Number]],
):
+ if not value:
+ return qs.none()
+
one_of = value.get("one_of")
range = value.get("range")
diff --git a/saleor/order/migrations/0208_order_lines_count_idx.py b/saleor/order/migrations/0208_order_lines_count_idx.py
new file mode 100644
index 0000000000..9db74d07f4
--- /dev/null
+++ b/saleor/order/migrations/0208_order_lines_count_idx.py
@@ -0,0 +1,28 @@
+# Generated by Django 5.2.1 on 2025-06-10 09:24
+
+
+from django.contrib.postgres.indexes import BTreeIndex
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("account", "0087_alter_address_metadata_and_more"),
+ ("channel", "0022_merge_20250527_1210"),
+ ("discount", "0086_merge_20250527_1210"),
+ ("giftcard", "0022_merge_20250527_1210"),
+ ("order", "0207_fulfillment_fulfillment_status_idx"),
+ ("shipping", "0036_merge_20250527_1210"),
+ ("tax", "0011_merge_20250530_0929"),
+ ("warehouse", "0035_alter_warehouse_metadata_and_more"),
+ ]
+
+ operations = [
+ AddIndexConcurrently(
+ model_name="order",
+ index=BTreeIndex(fields=["lines_count"], name="lines_count_idx"),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 744970d9d3..db5ea1c2ef 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -395,6 +395,7 @@ class Order(ModelWithMetadata, ModelWithExternalReference):
name="order_user_email_user_id_idx",
),
BTreeIndex(fields=["checkout_token"], name="checkout_token_btree_idx"),
+ BTreeIndex(fields=["lines_count"], name="lines_count_idx"),
]
def is_fully_paid(self):