Skip to content

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

Do not call automatic checkout completion when checkout contains unavailable lines (#16943)

Iga Karbowiak40b3e69b9b2c
diff --git a/saleor/checkout/tasks.py b/saleor/checkout/tasks.py
index 9b17170a11..f7b790256d 100644
--- a/saleor/checkout/tasks.py
+++ b/saleor/checkout/tasks.py
@@ -144,9 +144,27 @@ def automatic_checkout_completion_task(
app = App.objects.filter(pk=app_id).first()
manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout)
+ lines, unavailable_variant_pks = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
+ if unavailable_variant_pks:
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ not_available_variants_ids = {
+ graphene.Node.to_global_id("ProductVariant", pk)
+ for pk in unavailable_variant_pks
+ }
+ task_logger.info(
+ "The automatic checkout completion not triggered, as the checkout %s "
+ "contains unavailable variants: %s.",
+ checkout_id,
+ ", ".join(not_available_variants_ids),
+ extra={
+ "checkout_id": checkout_id,
+ "variant_ids": not_available_variants_ids,
+ },
+ )
+ return
+
task_logger.info(
"Automatic checkout completion triggered for checkout: %s.",
checkout_id,
diff --git a/saleor/checkout/tests/test_tasks.py b/saleor/checkout/tests/test_tasks.py
index 95ff718d38..c83860d7fb 100644
--- a/saleor/checkout/tests/test_tasks.py
+++ b/saleor/checkout/tests/test_tasks.py
@@ -571,6 +571,48 @@ def test_automatic_checkout_completion_task_missing_checkout(checkout, caplog):
assert not caplog.records
+def test_automatic_checkout_completion_task_unavailable_variant(
+ checkout_with_prices,
+ transaction_item_generator,
+ app,
+ caplog,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ checkout = checkout_with_prices
+ checkout_pk = checkout.pk
+
+ # make the checkout line unavailable
+ line = checkout.lines.first()
+ variant = line.variant
+ product = line.variant.product
+ product.channel_listings.update(is_published=False)
+
+ # allow catching the log in caplog
+ parent_logger = task_logger.parent
+ parent_logger.propagate = True
+
+ transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=checkout.total.gross.amount
+ )
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ automatic_checkout_completion_task(checkout_pk, None, app.id)
+
+ # then
+ assert Checkout.objects.filter(pk=checkout_pk).exists()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ assert len(caplog.records) == 1
+ assert caplog.records[0].message == (
+ "The automatic checkout completion not triggered, as the checkout "
+ f"{checkout_id} contains unavailable variants: {variant_id}."
+ )
+ assert caplog.records[0].checkout_id == checkout_id
+ assert caplog.records[0].levelno == logging.INFO
+
+
def test_automatic_checkout_completion_task_error_raised(checkout, app, caplog):
# given
checkout_pk = checkout.pk