Skip to content

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

add remove_shipping_method (#13068)

FremahAfcccfe47caab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 70ec038434..36b05090c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -56,7 +56,9 @@ All notable, unreleased changes to this project will be documented in this file.
2. Billing address
3. Channel's default country
+
### GraphQL API
+Shipping methods can be removed by the user after it has been assigned to a checkout; `shippingMethodId` is now a nullable input in the `checkoutShippingMethodUpdate` mutation. - #13068 by @FremahA
- Add `lines` to `OrderGrantedRefund` - #13014 by @korycins
- Add `orderNoteAdd` and `orderNoteUpdate` mutations and deprecate `orderAddNote` mutation - #12434 by @pawelzar
- Deprecate `Order.trackingClientId` field - #13146 by @SzymJ
diff --git a/saleor/graphql/checkout/mutations/checkout_shipping_method_update.py b/saleor/graphql/checkout/mutations/checkout_shipping_method_update.py
index 7d6a739707..549b35b511 100644
--- a/saleor/graphql/checkout/mutations/checkout_shipping_method_update.py
+++ b/saleor/graphql/checkout/mutations/checkout_shipping_method_update.py
@@ -48,7 +48,9 @@ class CheckoutShippingMethodUpdate(BaseMutation):
f"The ID of the checkout. {DEPRECATED_IN_3X_INPUT} Use `id` instead."
),
)
- shipping_method_id = graphene.ID(required=True, description="Shipping method.")
+ shipping_method_id = graphene.ID(
+ required=False, default_value=None, description="Shipping method."
+ )
class Meta:
description = "Updates the shipping method of the checkout."
@@ -91,7 +93,7 @@ class CheckoutShippingMethodUpdate(BaseMutation):
return str_type
@classmethod
- def perform_mutation( # type: ignore[override]
+ def perform_mutation(
cls,
_root,
info: ResolveInfo,
@@ -99,7 +101,7 @@ class CheckoutShippingMethodUpdate(BaseMutation):
*,
checkout_id=None,
id=None,
- shipping_method_id,
+ shipping_method_id=None,
token=None,
):
checkout = get_checkout(cls, info, checkout_id=checkout_id, token=token, id=id)
@@ -131,6 +133,8 @@ class CheckoutShippingMethodUpdate(BaseMutation):
)
}
)
+ if shipping_method_id is None:
+ return cls.remove_shipping_method(checkout, checkout_info, lines, manager)
type_name = cls._resolve_delivery_method_type(shipping_method_id)
@@ -253,3 +257,21 @@ class CheckoutShippingMethodUpdate(BaseMutation):
cls.call_event(manager.checkout_updated, checkout)
return CheckoutShippingMethodUpdate(checkout=checkout)
+
+ @classmethod
+ def remove_shipping_method(cls, checkout, checkout_info, lines, manager):
+ checkout.shipping_method = None
+ delete_external_shipping_id(checkout=checkout)
+ invalidate_prices_updated_fields = invalidate_checkout_prices(
+ checkout_info, lines, manager, save=False
+ )
+ checkout.save(
+ update_fields=[
+ "shipping_method",
+ ]
+ + invalidate_prices_updated_fields
+ )
+ get_checkout_metadata(checkout).save()
+
+ cls.call_event(manager.checkout_updated, checkout)
+ return CheckoutShippingMethodUpdate(checkout=checkout)
diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
index caa957abcf..6c59f7d209 100644
--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
@@ -448,7 +448,7 @@ def test_add_shipping_to_checkout(
FRAGMENT_CHECKOUT
+ """
mutation updateCheckoutShippingOptions(
- $id: ID, $shippingMethodId: ID!
+ $id: ID, $shippingMethodId: ID
) {
checkoutShippingMethodUpdate(
id: $id, shippingMethodId: $shippingMethodId
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update.py
index 8f36fb1cbf..cdcf8fa834 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update.py
@@ -21,7 +21,7 @@ from ....tests.utils import get_graphql_content
MUTATION_UPDATE_SHIPPING_METHOD = """
mutation checkoutShippingMethodUpdate(
- $id: ID, $shippingMethodId: ID!){
+ $id: ID, $shippingMethodId: ID){
checkoutShippingMethodUpdate(
id: $id, shippingMethodId: $shippingMethodId) {
errors {
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update_nullable_shipping_method_id.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update_nullable_shipping_method_id.py
new file mode 100644
index 0000000000..59ae94a3f6
--- /dev/null
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_method_update_nullable_shipping_method_id.py
@@ -0,0 +1,100 @@
+from unittest.mock import patch
+
+import graphene
+import pytest
+
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
+
+MUTATION_UPDATE_SHIPPING_METHOD = """
+ mutation checkoutShippingMethodUpdate(
+ $id: ID, $shippingMethodId: ID
+ ) {
+ checkoutShippingMethodUpdate(
+ id: $id, shippingMethodId: $shippingMethodId
+ ) {
+ errors {
+ field
+ message
+ code
+ }
+ checkout {
+ id
+ token
+ voucherCode
+ shippingMethod {
+ id
+ }
+ shippingPrice {
+ net {
+ amount
+ }
+ gross {
+ amount
+ }
+ }
+ }
+ }
+ }
+"""
+
+
+@patch(
+ "saleor.graphql.checkout.mutations.checkout_shipping_method_update."
+ "clean_delivery_method"
+)
+def test_checkout_shipping_method_update_nullable_shipping_method_id(
+ mock_clean_delivery_method,
+ staff_api_client,
+ shipping_method,
+ checkout_with_item_and_voucher_and_shipping_method,
+):
+ # Set up the initial state of the checkout
+ checkout = checkout_with_item_and_voucher_and_shipping_method
+
+ # Mock the clean_delivery_method function
+ mock_clean_delivery_method.return_value = True
+
+ # Execute the mutation to remove the shipping method from the checkout
+ response = staff_api_client.post_graphql(
+ MUTATION_UPDATE_SHIPPING_METHOD,
+ variables={"id": to_global_id_or_none(checkout), "shippingMethodId": None},
+ )
+
+ # Assert that the mutation was successful
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+ errors = data["errors"]
+ assert not errors
+ assert data["checkout"]["shippingMethod"] is None
+
+ # Ensure the shipping method was removed from the checkout
+ checkout.refresh_from_db(fields=["shipping_method"])
+ assert checkout.shipping_method is None
+
+ # Assert that the shipping price is cleared
+ assert checkout.shipping_price.net.amount == 0
+ assert checkout.shipping_price.gross.amount == 0
+
+ # Assert that the voucher is still assigned to the checkout
+ assert checkout.voucher_code is not None
+
+ # Add the shipping method back to the checkout
+ response = staff_api_client.post_graphql(
+ MUTATION_UPDATE_SHIPPING_METHOD,
+ variables={
+ "id": to_global_id_or_none(checkout),
+ "shippingMethodId": graphene.Node.to_global_id(
+ "ShippingMethod", shipping_method.pk
+ ),
+ },
+ )
+
+ # Assert that the mutation was successful
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+ errors = data["errors"]
+ assert not errors
+
+ # Ensure the shipping method was added back to the checkout
+ checkout.refresh_from_db(fields=["shipping_method"])
+ assert checkout.shipping_method is not None
diff --git a/saleor/graphql/checkout/tests/test_checkout_price_expiration.py b/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
index 8cab31750f..51d2290503 100644
--- a/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
+++ b/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
@@ -271,7 +271,7 @@ def test_checkout_billing_address_update_invalidate_prices(
UPDATE_CHECKOUT_SHIPPING_METHOD = """
-mutation updateCheckoutShippingOptions($token: UUID!, $shippingMethodId: ID!) {
+mutation updateCheckoutShippingOptions($token: UUID!, $shippingMethodId: ID) {
checkoutShippingMethodUpdate(token: $token, shippingMethodId: $shippingMethodId) {
errors {
field
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 1e7db44f5f..5f902791c6 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -16997,7 +16997,7 @@ type Mutation {
id: ID
"""Shipping method."""
- shippingMethodId: ID!
+ shippingMethodId: ID
"""
Checkout token.
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index e35811e097..586558ebb9 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -485,6 +485,16 @@ def checkout_with_item_and_shipping_method(checkout_with_item, shipping_method):
return checkout
+def checkout_with_item_and_voucher_and_shipping_method(
+ checkout_with_item_and_voucher, shipping_method
+):
+ checkout = checkout_with_item_and_voucher
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ return checkout
+
+
@pytest.fixture
def other_shipping_method(shipping_zone, channel_USD):
method = ShippingMethod.objects.create(