Skip to content

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

Fix fetching the `checkout.availableCollectionPoints` (#11489)

Iga Karbowiakfa7609225cdc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e34fdf1c19..fa2de87110 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `sortBy` argument to `Category.products`
### Other changes
+- Fix fetching the `checkout.availableCollectionPoints` - #11489 by @IKarbowiak
- Move checkout metadata to separate model - #11264 by @jakubkuc
- Add ability to set a custom Celery queue for async webhook - #11511 by @NyanKiyoshi
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 4ea3dcc59c..e68ce424b5 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -818,6 +818,36 @@ def test_checkout_available_collection_points_with_line_avail_in_2_local_and_1_a
assert all(c in expected_collection_points for c in received_collection_points)
+def test_checkout_available_collection_points_two_lines_for_same_checkout(
+ api_client, checkout_with_items_for_cc, stocks_for_cc
+):
+ # given
+ expected_collection_points = [
+ {"address": {"streetAddress1": "Tęczowa 7"}, "name": "Warehouse4"},
+ {"address": {"streetAddress1": "Tęczowa 7"}, "name": "Warehouse2"},
+ ]
+
+ line = checkout_with_items_for_cc.lines.first()
+ line_2 = checkout_with_items_for_cc.lines.all()[1]
+ line_2.variant = line.variant
+ line_2.save(update_fields=["variant"])
+
+ query = GET_CHECKOUT_AVAILABLE_COLLECTION_POINTS
+ variables = {"id": to_global_id_or_none(checkout_with_items_for_cc)}
+
+ # when
+ response = api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ received_collection_points = content["data"]["checkout"][
+ "availableCollectionPoints"
+ ]
+
+ assert len(received_collection_points) == len(expected_collection_points)
+ assert all(c in expected_collection_points for c in received_collection_points)
+
+
def test_checkout_avail_collect_points_exceeded_quantity_shows_only_all_warehouse(
api_client, checkout_with_items_for_cc, stocks_for_cc
):
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index 955868c743..b6979b687f 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -89,7 +89,6 @@ class WarehouseQueryset(models.QuerySet):
For `WarehouseClickAndCollect.LOCAL` all `CheckoutLine`s must be available from
a single warehouse.
"""
-
if all(
line.variant.is_preorder_active()
for line in lines_qs.select_related("variant").only("variant_id")
@@ -98,8 +97,10 @@ class WarehouseQueryset(models.QuerySet):
lines_quantity = (
lines_qs.filter(variant_id=OuterRef("product_variant_id"))
+ .order_by("variant_id")
+ .values("variant_id")
.annotate(prod_sum=Sum("quantity"))
- .values_list("prod_sum")
+ .values("prod_sum")
)
stocks_qs = (
@@ -123,13 +124,17 @@ class WarehouseQueryset(models.QuerySet):
) -> QuerySet["Warehouse"]:
warehouse_cc_option_enum = WarehouseClickAndCollectOption
+ number_of_variants = (
+ lines_qs.order_by("variant_id").distinct("variant_id").count()
+ )
+
return (
self.for_channel(channel_id)
.prefetch_related(Prefetch("stock_set", queryset=stocks_qs))
.filter(stock__in=stocks_qs)
.annotate(stock_num=Count("stock__id", distinct=True))
.filter(
- Q(stock_num=lines_qs.count())
+ Q(stock_num=number_of_variants)
& Q(click_and_collect_option=warehouse_cc_option_enum.LOCAL_STOCK)
| Q(click_and_collect_option=warehouse_cc_option_enum.ALL_WAREHOUSES)
)