Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Allow to use CheckoutDelivery with checkoutDeliveryMethodUpdate (#18940)
Maciej Korycinski1dbb64493dd2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 368a4aa4f4..02dd654e7c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -47,6 +47,7 @@ All notable, unreleased changes to this project will be documented in this file.- `CheckoutProblemDeliveryMethodInvalid`: the selected delivery method is no longer valid (e.g., the shipping address no longer falls within it). This problem blocks `checkoutComplete` until a valid delivery method is assigned via `checkoutDeliveryMethodUpdate`.See the [upgrading guide](https://docs.saleor.io/upgrade-guides/3-22-to-3-23##explicit-delivery-options-calculation) to learn more.+ - `checkoutDeliveryMethodUpdate` mutation now accepts `CheckoutDelivery` ID as `deliveryMethodId` (ID returned by `deliveryOptionsCalculate` mutation). Usage of `ShippingMethod` ID is deprecated in favor of `CheckoutDelivery` ID.diff --git a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.pyindex 0e0a45d62f..f552e707f1 100644--- a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py+++ b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py@@ -52,7 +52,11 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):)delivery_method_id = graphene.ID(- description="Delivery Method ID (`Warehouse` ID or `ShippingMethod` ID).",+ description=(+ "Delivery Method ID (`Warehouse` ID or `ShippingMethod` ID or "+ "`CheckoutDelivery` ID).\n\nDEPRECATED: Usage of `ShippingMethod` ID "+ "is deprecated. Use CheckoutDelivery ID instead."+ ),required=False,)@@ -117,7 +121,12 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):if id_ is None:return None, None- possible_types = ("Warehouse", "ShippingMethod", APP_ID_PREFIX)+ possible_types = (+ "Warehouse",+ "ShippingMethod",+ "CheckoutDelivery",+ APP_ID_PREFIX,+ )type_, id_ = from_global_id_or_error(id_)str_type = str(type_)@@ -125,7 +134,7 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):raise ValidationError({"delivery_method_id": ValidationError(- "ID does not belong to Warehouse or ShippingMethod",+ "ID does not belong to Warehouse, ShippingMethod, or CheckoutDelivery",code=CheckoutErrorCode.INVALID.value,)}@@ -137,6 +146,7 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):def get_checkout_delivery(cls,checkout_info: CheckoutInfo,+ type_name: str,internal_shipping_method_id: str | None,requestor: Union["App", "User", None],) -> CheckoutDelivery | None:@@ -146,12 +156,19 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):checkout_deliveries = get_or_fetch_checkout_deliveries(checkout_info, requestor=requestor).get()+for method in checkout_deliveries:if not method.active:continueif method.shipping_method_id == internal_shipping_method_id:return method+ if (+ type_name == "CheckoutDelivery"+ and str(method.id) == internal_shipping_method_id+ ):+ return method+raise ValidationError({"delivery_method_id": ValidationError(@@ -175,13 +192,14 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):None)type_name, internal_id = cls._resolve_delivery_method_type(delivery_method_id)- if internal_id is None:++ if internal_id is None or type_name is None:return Noneif type_name == "Warehouse":delivery_method_data = cls.get_collection_point(checkout_info, internal_id)else:delivery_method_data = cls.get_checkout_delivery(- checkout_info, internal_id, requestor=requestor+ checkout_info, type_name, internal_id, requestor=requestor)return delivery_method_datadiff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.pyindex 66fb90acf9..7dad921a2a 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py@@ -1,10 +1,13 @@+from datetime import timedeltafrom decimal import Decimalfrom unittest import mockfrom unittest.mock import ANY, patch+from uuid import uuid4import grapheneimport pytestfrom django.test import override_settings+from django.utils import timezonefrom .....account.models import Addressfrom .....checkout.actions import call_checkout_info_event@@ -33,6 +36,9 @@ MUTATION_UPDATE_DELIVERY_METHOD = """idfirstName}+ delivery {+ id+ }deliveryMethod {__typename... on ShippingMethod {@@ -2069,3 +2075,132 @@ def test_checkout_delivery_method_update_from_built_in_shipping_to_none(mocked_invalidate_checkout.assert_called_once()mocked_call_checkout_info_event.assert_called_once()+++MUTATION_UPDATE_DELIVERY_METHOD_WITH_ONLY_DELIVERY = """+ mutation checkoutDeliveryMethodUpdate($id: ID, $deliveryMethodId: ID) {+ checkoutDeliveryMethodUpdate(id: $id, deliveryMethodId: $deliveryMethodId) {+ checkout {+ id+ delivery {+ id+ }+ }+ errors {+ field+ message+ code+ }+ }+ }+"""+++ "saleor.checkout.actions.call_checkout_info_event",+ wraps=call_checkout_info_event,+)+ "saleor.checkout.utils.invalidate_checkout",+ wraps=invalidate_checkout,+)+def test_with_checkout_delivery_as_id(+ mocked_invalidate_checkout,+ mocked_call_checkout_info_event,+ checkout_with_delivery_method_for_external_shipping,+ checkout_delivery,+ api_client,+):+ # given+ checkout = checkout_with_delivery_method_for_external_shipping+ checkout.delivery_methods_stale_at = timezone.now() + timedelta(minutes=5)+ checkout.save_billing_address = True+ checkout.save_shipping_address = True+ checkout.save(+ update_fields=[+ "save_billing_address",+ "save_shipping_address",+ "delivery_methods_stale_at",+ ]+ )+ delivery = checkout_delivery(checkout)++ # when+ response = api_client.post_graphql(+ MUTATION_UPDATE_DELIVERY_METHOD_WITH_ONLY_DELIVERY,+ {+ "id": to_global_id_or_none(checkout),+ "deliveryMethodId": to_global_id_or_none(delivery),+ },+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ errors = data["errors"]+ assert not errors+ assert data["checkout"]["delivery"]["id"] == to_global_id_or_none(delivery)++ checkout.refresh_from_db()++ assert checkout.collection_point_id is None+ assert checkout.assigned_delivery.id == delivery.id++ # Called as checkout has been changed by assigning new delivery+ mocked_invalidate_checkout.assert_called_once()+ mocked_call_checkout_info_event.assert_called_once()+++ "saleor.checkout.actions.call_checkout_info_event",+ wraps=call_checkout_info_event,+)+ "saleor.checkout.utils.invalidate_checkout",+ wraps=invalidate_checkout,+)+def test_with_invalid_checkout_delivery_as_id(+ mocked_invalidate_checkout,+ mocked_call_checkout_info_event,+ checkout_with_delivery_method_for_external_shipping,+ api_client,+):+ # given+ checkout = checkout_with_delivery_method_for_external_shipping+ checkout.delivery_methods_stale_at = timezone.now() + timedelta(minutes=5)+ checkout.save_billing_address = True+ checkout.save_shipping_address = True+ checkout.save(+ update_fields=[+ "save_billing_address",+ "save_shipping_address",+ "delivery_methods_stale_at",+ ]+ )++ non_existing_id = uuid4()++ # when+ response = api_client.post_graphql(+ MUTATION_UPDATE_DELIVERY_METHOD_WITH_ONLY_DELIVERY,+ {+ "id": to_global_id_or_none(checkout),+ "deliveryMethodId": graphene.Node.to_global_id(+ "CheckoutDelivery", non_existing_id+ ),+ },+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ errors = data["errors"]+ assert len(errors) == 1+ assert errors[0]["field"] == "deliveryMethodId"+ assert errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name++ checkout.refresh_from_db()++ assert checkout.collection_point_id is None+ assert checkout.assigned_delivery.id != non_existing_id++ assert not mocked_invalidate_checkout.called+ assert not mocked_call_checkout_info_event.calleddiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 90aeb33901..fab67ed477 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -20963,7 +20963,11 @@ type Mutation {- CHECKOUT_UPDATED (async): A checkout was updated."""checkoutDeliveryMethodUpdate(- """Delivery Method ID (`Warehouse` ID or `ShippingMethod` ID)."""+ """+ Delivery Method ID (`Warehouse` ID or `ShippingMethod` ID or `CheckoutDelivery` ID).++ DEPRECATED: Usage of `ShippingMethod` ID is deprecated. Use CheckoutDelivery ID instead.+ """deliveryMethodId: ID"""The checkout's ID."""