Skip to content

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

Allow to pass metadata directly to line data in checkoutCreate and checkoutLinesAdd mutations (#10592)

SzymJ577d43f21b7d
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index b4e01f9e7f..cd1c82ab6c 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -226,7 +226,9 @@ def add_variants_to_checkout(
if to_delete:
CheckoutLine.objects.filter(pk__in=[line.pk for line in to_delete]).delete()
if to_update:
- CheckoutLine.objects.bulk_update(to_update, ["quantity", "price_override"])
+ CheckoutLine.objects.bulk_update(
+ to_update, ["quantity", "price_override", "metadata"]
+ )
if to_create:
CheckoutLine.objects.bulk_create(to_create)
@@ -257,6 +259,10 @@ def _get_line_if_exist(line_data, lines_by_ids):
def _append_line_to_update(to_update, to_delete, line_data, replace, line):
+ if line_data.metadata_list:
+ line.store_value_in_metadata(
+ {data.key: data.value for data in line_data.metadata_list}
+ )
if line_data.quantity_to_update:
quantity = line_data.quantity
if quantity > 0:
@@ -281,15 +287,18 @@ def _append_line_to_delete(to_delete, line_data, line):
def _append_line_to_create(to_create, checkout, variant, line_data, line):
if line is None:
if line_data.quantity > 0:
- to_create.append(
- CheckoutLine(
- checkout=checkout,
- variant=variant,
- quantity=line_data.quantity,
- currency=checkout.currency,
- price_override=line_data.custom_price,
- )
+ checkout_line = CheckoutLine(
+ checkout=checkout,
+ variant=variant,
+ quantity=line_data.quantity,
+ currency=checkout.currency,
+ price_override=line_data.custom_price,
)
+ if line_data.metadata_list:
+ checkout_line.store_value_in_metadata(
+ {data.key: data.value for data in line_data.metadata_list}
+ )
+ to_create.append(checkout_line)
def _check_new_checkout_address(checkout, address, address_type):
diff --git a/saleor/graphql/checkout/mutations/checkout_create.py b/saleor/graphql/checkout/mutations/checkout_create.py
index 5185338f3f..d9bd082765 100644
--- a/saleor/graphql/checkout/mutations/checkout_create.py
+++ b/saleor/graphql/checkout/mutations/checkout_create.py
@@ -17,6 +17,7 @@ from ...core.descriptions import (
ADDED_IN_31,
ADDED_IN_35,
ADDED_IN_36,
+ ADDED_IN_38,
DEPRECATED_IN_3X_FIELD,
PREVIEW_FEATURE,
)
@@ -41,6 +42,8 @@ if TYPE_CHECKING:
from ....account.models import Address
from .utils import CheckoutLineData
+from ...meta.mutations import MetadataInput
+
class CheckoutAddressValidationRules(graphene.InputObjectType):
check_required_fields = graphene.Boolean(
@@ -106,6 +109,11 @@ class CheckoutLineInput(graphene.InputObjectType):
"by skipping the matching logic. " + ADDED_IN_36 + PREVIEW_FEATURE
),
)
+ metadata = NonNullList(
+ MetadataInput,
+ description=("Fields required to update the object's metadata." + ADDED_IN_38),
+ required=False,
+ )
class CheckoutCreateInput(graphene.InputObjectType):
diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.py
index cc0569b12e..3103af2e20 100644
--- a/saleor/graphql/checkout/mutations/utils.py
+++ b/saleor/graphql/checkout/mutations/utils.py
@@ -44,6 +44,7 @@ class CheckoutLineData:
quantity_to_update: bool = False
custom_price: Optional[Decimal] = None
custom_price_to_update: bool = False
+ metadata_list: Optional[list] = None
def clean_delivery_method(
@@ -311,10 +312,14 @@ 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")
+
_, variant_db_id = graphene.Node.from_global_id(variant_id)
if force_new_line:
- line_data = CheckoutLineData(variant_id=variant_db_id)
+ line_data = CheckoutLineData(
+ variant_id=variant_db_id, metadata_list=metadata_list
+ )
grouped_checkout_lines_data.append(line_data)
else:
_, variant_db_id = graphene.Node.from_global_id(variant_id)
@@ -327,6 +332,7 @@ 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
@@ -334,9 +340,16 @@ def group_lines_input_on_add(
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
+
# when variant already exist in multiple lines then create a new line
except ValidationError:
- line_data = CheckoutLineData(variant_id=variant_db_id)
+ line_data = CheckoutLineData(
+ variant_id=variant_db_id, metadata_list=metadata_list
+ )
grouped_checkout_lines_data.append(line_data)
if (quantity := line.get("quantity")) is not None:
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
index 5c1db4856d..9f11546af4 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
@@ -573,6 +573,45 @@ def test_checkout_create_with_custom_price(
assert checkout_line.price_override == price
+def test_checkout_create_with_metadata_in_line(
+ api_client, stock, graphql_address_data, channel_USD
+):
+ """Ensure that app with handle checkouts permission can set custom price."""
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ metadata_key = "md key"
+ metadata_value = "md value"
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [
+ {
+ "quantity": 1,
+ "variantId": variant_id,
+ "metadata": [{"key": metadata_key, "value": metadata_value}],
+ }
+ ],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ content = get_graphql_content(response)["data"]["checkoutCreate"]
+
+ new_checkout = Checkout.objects.first()
+ assert new_checkout is not None
+ checkout_data = content["checkout"]
+ assert checkout_data["token"] == str(new_checkout.token)
+ assert new_checkout.lines.count() == 1
+ checkout_line = new_checkout.lines.first()
+ assert checkout_line.variant == variant
+ assert checkout_line.quantity == 1
+ assert checkout_line.metadata == {metadata_key: metadata_value}
+
+
def test_checkout_create_with_custom_price_duplicated_items(
app_api_client,
stock,
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 42a2181375..4b84f88e42 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -100,6 +100,95 @@ def test_checkout_lines_add(
assert mocked_invalidate_checkout_prices.call_count == 1
+def test_checkout_lines_add_with_existing_variant_and_metadata(
+ user_api_client,
+ checkout_with_item,
+ stock,
+):
+ # given
+ checkout = checkout_with_item
+
+ old_meta = {"old_key": "old_value"}
+ metadata_key = "md key"
+ metadata_value = "md 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": [{"key": metadata_key, "value": metadata_value}],
+ }
+ ],
+ "channelSlug": checkout.channel.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesAdd"]
+ checkout.refresh_from_db()
+ line.refresh_from_db()
+
+ # then
+ assert not data["errors"]
+ assert line.quantity == 4
+ assert line.metadata == {**old_meta, **{metadata_key: metadata_value}}
+
+
+def test_checkout_lines_add_with_new_variant_and_metadata(
+ user_api_client,
+ checkout_with_item,
+ stock,
+):
+ # given
+ variant = stock.product_variant
+
+ checkout = checkout_with_item
+
+ metadata_key = "md key"
+ metadata_value = "md value"
+
+ lines, _ = fetch_checkout_lines(checkout)
+ assert calculate_checkout_quantity(lines) == 3
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "lines": [
+ {
+ "variantId": variant_id,
+ "quantity": 1,
+ "metadata": [{"key": metadata_key, "value": metadata_value}],
+ }
+ ],
+ "channelSlug": checkout.channel.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutLinesAdd"]
+ checkout.refresh_from_db()
+ line = checkout.lines.last()
+
+ # then
+ assert not data["errors"]
+ assert checkout.lines.count() == 2
+ assert line.variant == variant
+ assert line.quantity == 1
+ assert line.metadata == {metadata_key: metadata_value}
+
+
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_add."
"update_checkout_shipping_method_if_invalid",
diff --git a/saleor/graphql/core/descriptions.py b/saleor/graphql/core/descriptions.py
index 3acca7b5b5..1a14d14e23 100644
--- a/saleor/graphql/core/descriptions.py
+++ b/saleor/graphql/core/descriptions.py
@@ -18,6 +18,7 @@ ADDED_IN_34 = "\n\nAdded in Saleor 3.4."
ADDED_IN_35 = "\n\nAdded in Saleor 3.5."
ADDED_IN_36 = "\n\nAdded in Saleor 3.6."
ADDED_IN_37 = "\n\nAdded in Saleor 3.7."
+ADDED_IN_38 = "\n\nAdded in Saleor 3.8."
PREVIEW_FEATURE = (
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 69d2a8eaac..0b9f78062a 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -19610,6 +19610,13 @@ input CheckoutLineInput {
Note: this API is currently in Feature Preview and can be subject to changes at later point.
"""
forceNewLine: Boolean = false
+
+ """
+ Fields required to update the object's metadata.
+
+ Added in Saleor 3.8.
+ """
+ metadata: [MetadataInput!]
}
input CheckoutValidationRules {