Skip to content

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

Block CheckoutLinesUpdate for creating empty lines

fowczrekbe92d97a9b1e
diff --git a/saleor/checkout/migrations/0037_remove_empty_lines.py b/saleor/checkout/migrations/0037_remove_empty_lines.py
new file mode 100644
index 0000000000..0a3d691dc6
--- /dev/null
+++ b/saleor/checkout/migrations/0037_remove_empty_lines.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.2.6 on 2021-09-03 09:16
+
+from django.db import migrations
+
+
+def remove_empty_lines(apps, schema_editor):
+ CheckoutLine = apps.get_model("checkout", "CheckoutLine")
+ CheckoutLine.objects.filter(quantity=0).delete()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("checkout", "0036_alter_checkout_language_code"),
+ ]
+
+ operations = [
+ migrations.RunPython(remove_empty_lines, migrations.RunPython.noop),
+ ]
diff --git a/saleor/checkout/migrations/0038_merge_20210903_1048.py b/saleor/checkout/migrations/0038_merge_20210903_1048.py
new file mode 100644
index 0000000000..4cb1f0bac7
--- /dev/null
+++ b/saleor/checkout/migrations/0038_merge_20210903_1048.py
@@ -0,0 +1,13 @@
+# Generated by Django 3.2.6 on 2021-09-03 10:48
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("checkout", "0037_merge_20210903_1013"),
+ ("checkout", "0037_remove_empty_lines"),
+ ]
+
+ operations = []
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 8e6c190af7..5c83f72149 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -167,7 +167,7 @@ def add_variants_to_checkout(
to_update.append(line)
else:
to_delete.append(line)
- else:
+ elif quantity > 0:
to_create.append(
CheckoutLine(checkout=checkout, variant=variant, quantity=quantity)
)
diff --git a/saleor/graphql/checkout/tests/test_checkout_lines.py b/saleor/graphql/checkout/tests/test_checkout_lines.py
index 15ac368eac..0dd19ef283 100644
--- a/saleor/graphql/checkout/tests/test_checkout_lines.py
+++ b/saleor/graphql/checkout/tests/test_checkout_lines.py
@@ -540,6 +540,40 @@ def test_checkout_line_delete_by_zero_quantity(
mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+ "saleor.graphql.checkout.mutations.update_checkout_shipping_method_if_invalid",
+ wraps=update_checkout_shipping_method_if_invalid,
+)
+def test_checkout_line_update_by_zero_quantity_dont_create_new_lines(
+ mocked_update_shipping_method,
+ user_api_client,
+ checkout_with_item,
+):
+ checkout = checkout_with_item
+ line = checkout.lines.first()
+ variant = line.variant
+ checkout.lines.all().delete()
+ assert checkout.lines.count() == 0
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ variables = {
+ "token": checkout.token,
+ "lines": [{"variantId": variant_id, "quantity": 0}],
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
+ content = get_graphql_content(response)
+
+ data = content["data"]["checkoutLinesUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.lines.count() == 0
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+
+
def test_checkout_lines_update_with_unpublished_product(
user_api_client, checkout_with_item, channel_USD
):