Skip to content

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

Fix CollectionsByProductIdLoader if collection is missing (#16423)

Artur268793c7477d
diff --git a/saleor/discount/utils/voucher.py b/saleor/discount/utils/voucher.py
index 80e14706dd..3cce524684 100644
--- a/saleor/discount/utils/voucher.py
+++ b/saleor/discount/utils/voucher.py
@@ -208,7 +208,7 @@ def get_discounted_lines(
continue
line_category = line_product.category
line_collections = set(
- [collection.pk for collection in line_info.collections]
+ [collection.pk for collection in line_info.collections if collection]
)
if line_info.variant and (
line_variant.pk in voucher_info.variant_pks
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index 8905b1f3e5..81a55c3926 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -137,7 +137,11 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader[str, list[CheckoutLineIn
product_type=product_types_map[line.variant_id],
collections=sorted(
collections_map[line.variant_id],
- key=lambda collection: collection.slug,
+ key=(
+ lambda collection: collection.slug
+ if collection
+ else ""
+ ),
),
discounts=checkout_lines_discounts[line.id],
tax_class=tax_class_map[line.variant_id],
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
index 497ea79b48..ec44e7c982 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
@@ -3,6 +3,7 @@ from decimal import Decimal
from unittest import mock
from unittest.mock import call, patch
+import before_after
import graphene
import pytest
from django.test import override_settings
@@ -17,7 +18,7 @@ from .....checkout.utils import add_variant_to_checkout, set_external_shipping_i
from .....core.models import EventDelivery
from .....discount import VoucherType
from .....plugins.manager import get_plugins_manager
-from .....product.models import ProductVariantChannelListing
+from .....product.models import Collection, ProductVariantChannelListing
from .....warehouse.models import Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
@@ -506,6 +507,42 @@ def test_checkout_add_collection_voucher_code_checkout_with_sale(
assert checkout.discount_amount == voucher_discount
+def test_checkout_add_collection_voucher_code_checkout_with_sale_collection_deleted(
+ api_client, checkout_with_item_on_sale, voucher_percentage, collection
+):
+ # given
+ checkout = checkout_with_item_on_sale
+
+ voucher = voucher_percentage
+ product = checkout.lines.first().variant.product
+ product.collections.add(collection)
+ voucher.type = VoucherType.SPECIFIC_PRODUCT
+ voucher.save()
+ voucher.collections.add(collection)
+ checkout.price_expiration = timezone.now()
+ lines, _ = fetch_checkout_lines(checkout)
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "promoCode": voucher.code,
+ }
+ assert Collection.objects.count() == 1
+
+ # when
+ def delete_collections(*args, **kwargs):
+ Collection.objects.all().delete()
+
+ with before_after.after(
+ "saleor.graphql.product.dataloaders.products.CollectionsByProductIdLoader"
+ ".batch_load",
+ delete_collections,
+ ):
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ # then
+ assert not data["errors"]
+ assert Collection.objects.count() == 0
+
+
def test_checkout_add_category_code_checkout_with_sale(
api_client, checkout_with_item_on_sale, voucher_percentage, channel_USD
):
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.py
index fd085b8fee..2aced0e8c4 100644
--- a/saleor/graphql/product/dataloaders/products.py
+++ b/saleor/graphql/product/dataloaders/products.py
@@ -524,10 +524,8 @@ class CollectionByIdLoader(DataLoader):
context_key = "collection_by_id"
def batch_load(self, keys):
- collections = (
- Collection.objects.using(self.database_connection_name)
- .using(self.database_connection_name)
- .in_bulk(keys)
+ collections = Collection.objects.using(self.database_connection_name).in_bulk(
+ keys
)
return [collections.get(collection_id) for collection_id in keys]
@@ -538,7 +536,6 @@ class CollectionsByProductIdLoader(DataLoader):
def batch_load(self, keys):
product_collection_pairs = list(
CollectionProduct.objects.using(self.database_connection_name)
- .using(self.database_connection_name)
.filter(product_id__in=keys)
.order_by("id")
.values_list("product_id", "collection_id")
@@ -549,9 +546,10 @@ class CollectionsByProductIdLoader(DataLoader):
product_collection_map[pid].append(cid)
def map_collections(collections):
- collection_map = {c.id: c for c in collections}
+ collection_map = {c.id: c for c in collections if c is not None}
+
return [
- [collection_map[cid] for cid in product_collection_map[pid]]
+ [collection_map.get(cid) for cid in product_collection_map[pid]]
for pid in keys
]