Skip to content

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

Improve checkout lines metadata overwriting conditions (#17552)

Lukasz Ostrowski839b2df1a710
diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.py
index 17cf080068..faa916a777 100644
--- a/saleor/graphql/checkout/mutations/utils.py
+++ b/saleor/graphql/checkout/mutations/utils.py
@@ -2,7 +2,7 @@ import datetime
import uuid
from collections import defaultdict
from collections.abc import Iterable
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from decimal import Decimal
from typing import TYPE_CHECKING, Any, cast
@@ -68,7 +68,7 @@ class CheckoutLineData:
quantity_to_update: bool = False
custom_price: Decimal | None = None
custom_price_to_update: bool = False
- metadata_list: list | None = None
+ metadata_list: list = field(default_factory=list)
def clean_delivery_method(
@@ -376,13 +376,13 @@ def group_lines_input_on_add(
for line in lines:
variant_id = cast(str, line.get("variant_id"))
force_new_line = line.get("force_new_line")
- metadata_list = line.get("metadata")
+ metadata_list_from_input = line.get("metadata", [])
_, variant_db_id = graphene.Node.from_global_id(variant_id)
if force_new_line:
line_data = CheckoutLineData(
- variant_id=variant_db_id, metadata_list=metadata_list
+ variant_id=variant_db_id, metadata_list=metadata_list_from_input
)
grouped_checkout_lines_data.append(line_data)
else:
@@ -395,24 +395,22 @@ def group_lines_input_on_add(
if not line_db_id:
line_data = checkout_lines_data_map[variant_db_id]
+
line_data.variant_id = variant_db_id
- line_data.metadata_list = metadata_list
else:
line_data = checkout_lines_data_map[line_db_id]
+
line_data.line_id = line_db_id
line_data.variant_id = find_variant_id_when_line_parameter_used(
line_db_id, existing_lines_info
)
- if line_data.metadata_list and metadata_list:
- line_data.metadata_list += metadata_list
- else:
- line_data.metadata_list = metadata_list
+ line_data.metadata_list += metadata_list_from_input
# when variant already exist in multiple lines then create a new line
except ValidationError:
line_data = CheckoutLineData(
- variant_id=variant_db_id, metadata_list=metadata_list
+ variant_id=variant_db_id, metadata_list=metadata_list_from_input
)
grouped_checkout_lines_data.append(line_data)
@@ -443,7 +441,7 @@ def group_lines_input_data_on_update(
for line in lines:
variant_id = cast(str, line.get("variant_id"))
line_id = cast(str, line.get("line_id"))
- metadata_list = line.get("metadata")
+ metadata_list_from_input = line.get("metadata", [])
line_db_id, variant_db_id = None, None
if line_id:
@@ -473,10 +471,7 @@ def group_lines_input_data_on_update(
line_data.custom_price = line["price"]
line_data.custom_price_to_update = True
- if line_data.metadata_list and metadata_list:
- line_data.metadata_list += metadata_list
- else:
- line_data.metadata_list = metadata_list
+ line_data.metadata_list += metadata_list_from_input
grouped_checkout_lines_data += list(checkout_lines_data_map.values())
return grouped_checkout_lines_data
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
index 51d36b3d35..1942db27ef 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -779,6 +779,47 @@ def test_checkout_lines_add_with_invalid_metadata(
assert errors[0]["field"] == "metadata"
+def test_checkout_lines_add_with_empty_metadata_and_old_exist(
+ user_api_client,
+ checkout_with_item,
+ stock,
+):
+ # given
+ checkout = checkout_with_item
+
+ old_meta = {"old_key": "old_value"}
+
+ line = checkout.lines.first()
+ line.store_value_in_metadata(old_meta)
+ line.save(update_fields=["metadata"])
+
+ lines, _ = fetch_checkout_lines(checkout)
+ assert calculate_checkout_quantity(lines) == 3
+ variant_id = graphene.Node.to_global_id("ProductVariant", line.variant_id)
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "lines": [
+ {
+ "variantId": variant_id,
+ "quantity": 1,
+ "metadata": [],
+ }
+ ],
+ "channelSlug": checkout.channel.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ get_graphql_content(response)
+
+ # then
+ checkout.refresh_from_db()
+ line = checkout.lines.last()
+
+ assert line.metadata.get("old_key") == "old_value"
+
+
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_add."
"update_checkout_shipping_method_if_invalid",
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
index 94b1124fd1..f0d54ae9e5 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
@@ -1809,3 +1809,108 @@ def test_checkout_lines_update_with_invalid_metadata(
assert expected_error["field"] == "metadata"
assert expected_error["code"] == "REQUIRED"
+
+
+def test_checkout_lines_update_with_empty_metadata_preserve_old(
+ user_api_client,
+ checkout_with_item,
+):
+ # given
+ checkout = checkout_with_item
+
+ assert checkout.lines.count() == 1
+
+ line = checkout.lines.first()
+
+ line.metadata["test_key"] = "old_value"
+ line.save(update_fields=["metadata"])
+
+ variant = line.variant
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "lines": [
+ {
+ "variantId": variant_id,
+ "quantity": 1,
+ # Leave input empty to ensure it will not affect existing entries
+ "metadata": [],
+ }
+ ],
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
+
+ # then
+ content = get_graphql_content(response)
+
+ data = content["data"]["checkoutLinesUpdate"]
+
+ assert not data["errors"]
+
+ checkout.refresh_from_db()
+
+ assert checkout.lines.count() == 1
+
+ line = checkout.lines.first()
+
+ assert line.metadata["test_key"] == "old_value"
+
+ assert line.variant == variant
+
+
+def test_checkout_lines_update_with_new_metadata_merge_old(
+ user_api_client,
+ checkout_with_item,
+):
+ # given
+ checkout = checkout_with_item
+
+ assert checkout.lines.count() == 1
+
+ line = checkout.lines.first()
+
+ line.metadata["test_key"] = "old_value"
+ line.save(update_fields=["metadata"])
+
+ variant = line.variant
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ variables = {
+ "id": to_global_id_or_none(checkout_with_item),
+ "lines": [
+ {
+ "variantId": variant_id,
+ "quantity": 1,
+ "metadata": [
+ {
+ "key": "new_key",
+ "value": "new_value",
+ }
+ ],
+ }
+ ],
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
+
+ # then
+ content = get_graphql_content(response)
+
+ data = content["data"]["checkoutLinesUpdate"]
+
+ assert not data["errors"]
+
+ checkout.refresh_from_db()
+
+ assert checkout.lines.count() == 1
+
+ line = checkout.lines.first()
+
+ assert line.metadata["test_key"] == "old_value"
+ assert line.metadata["new_key"] == "new_value"
+
+ assert line.variant == variant