Skip to content

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

Allow setting shipping address in case CC was changed to standard delivery method (#17525)

Iga Karbowiak659af1b19239
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 7913086768..51875c40e3 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -1015,7 +1015,9 @@ def get_valid_collection_points_for_checkout(
)
-def clear_delivery_method(checkout_info: "CheckoutInfo"):
+def clear_delivery_method(
+ checkout_info: "CheckoutInfo", save: bool = True
+) -> list[str]:
checkout = checkout_info.checkout
updated_fields = remove_delivery_method_from_checkout(checkout_info.checkout)
@@ -1031,12 +1033,11 @@ def clear_delivery_method(checkout_info: "CheckoutInfo"):
shipping_channel_listings=checkout_info.shipping_channel_listings,
)
if updated_fields:
- checkout.save(
- update_fields=updated_fields
- + [
- "last_change",
- ]
- )
+ updated_fields.append("last_change")
+ if save:
+ checkout.save(update_fields=updated_fields)
+
+ return updated_fields
def is_fully_paid(
diff --git a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py b/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
index d7245bf63d..94f1c1c0c8 100644
--- a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/mutations/checkout_add_promo_code.py
@@ -114,14 +114,17 @@ class CheckoutAddPromoCode(BaseMutation):
shipping_channel_listings=shipping_channel_listings,
)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
- invalidate_checkout(
+ shipping_update_fields = update_checkout_shipping_method_if_invalid(
+ checkout_info, lines
+ )
+ invalidate_update_fields = invalidate_checkout(
checkout_info,
lines,
manager,
recalculate_discount=False,
- save=True,
+ save=False,
)
+ checkout.save(update_fields=shipping_update_fields + invalidate_update_fields)
call_checkout_info_event(
manager=manager,
event_name=WebhookEventAsyncType.CHECKOUT_UPDATED,
diff --git a/saleor/graphql/checkout/mutations/checkout_line_delete.py b/saleor/graphql/checkout/mutations/checkout_line_delete.py
index 48af48a0e7..d2a7581645 100644
--- a/saleor/graphql/checkout/mutations/checkout_line_delete.py
+++ b/saleor/graphql/checkout/mutations/checkout_line_delete.py
@@ -81,8 +81,13 @@ class CheckoutLineDelete(BaseMutation):
manager = get_plugin_manager_promise(info.context).get()
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
- invalidate_checkout(checkout_info, lines, manager, save=True)
+ shipping_update_fields = update_checkout_shipping_method_if_invalid(
+ checkout_info, lines
+ )
+ invalidate_update_fields = invalidate_checkout(
+ checkout_info, lines, manager, save=False
+ )
+ checkout.save(update_fields=shipping_update_fields + invalidate_update_fields)
call_checkout_info_event(
manager,
event_name=WebhookEventAsyncType.CHECKOUT_UPDATED,
diff --git a/saleor/graphql/checkout/mutations/checkout_lines_add.py b/saleor/graphql/checkout/mutations/checkout_lines_add.py
index f50cd3f44d..b3ee96133b 100644
--- a/saleor/graphql/checkout/mutations/checkout_lines_add.py
+++ b/saleor/graphql/checkout/mutations/checkout_lines_add.py
@@ -228,8 +228,13 @@ class CheckoutLinesAdd(BaseMutation):
)
update_checkout_external_shipping_method_if_invalid(checkout_info, lines)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
- invalidate_checkout(checkout_info, lines, manager, save=True)
+ shipping_update_fields = update_checkout_shipping_method_if_invalid(
+ checkout_info, lines
+ )
+ invalidate_update_fields = invalidate_checkout(
+ checkout_info, lines, manager, save=False
+ )
+ checkout.save(update_fields=shipping_update_fields + invalidate_update_fields)
call_checkout_info_event(
manager,
event_name=WebhookEventAsyncType.CHECKOUT_UPDATED,
diff --git a/saleor/graphql/checkout/mutations/checkout_lines_delete.py b/saleor/graphql/checkout/mutations/checkout_lines_delete.py
index 9912df33e8..90ff2442d0 100644
--- a/saleor/graphql/checkout/mutations/checkout_lines_delete.py
+++ b/saleor/graphql/checkout/mutations/checkout_lines_delete.py
@@ -100,8 +100,13 @@ class CheckoutLinesDelete(BaseMutation):
manager = get_plugin_manager_promise(info.context).get()
checkout_info = fetch_checkout_info(checkout, lines, manager)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
- invalidate_checkout(checkout_info, lines, manager, save=True)
+ shipping_update_fields = update_checkout_shipping_method_if_invalid(
+ checkout_info, lines
+ )
+ invalidate_update_fields = invalidate_checkout(
+ checkout_info, lines, manager, save=False
+ )
+ checkout.save(update_fields=shipping_update_fields + invalidate_update_fields)
call_checkout_info_event(
manager,
event_name=WebhookEventAsyncType.CHECKOUT_UPDATED,
diff --git a/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py b/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py
index 8202658809..45c8e05b94 100644
--- a/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py
+++ b/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py
@@ -195,8 +195,6 @@ class CheckoutShippingAddressUpdate(AddressMetadataMixin, BaseMutation, I18nMixi
checkout_info.get_delivery_method_info(),
)
- update_checkout_shipping_method_if_invalid(checkout_info, lines)
-
shipping_address_updated_fields = []
with traced_atomic_transaction():
shipping_address_instance.save()
@@ -207,12 +205,18 @@ class CheckoutShippingAddressUpdate(AddressMetadataMixin, BaseMutation, I18nMixi
lines,
shipping_channel_listings,
)
+
+ shipping_update_fields = update_checkout_shipping_method_if_invalid(
+ checkout_info, lines
+ )
+
invalidate_prices_updated_fields = invalidate_checkout(
checkout_info, lines, manager, save=False
)
checkout.save(
update_fields=shipping_address_updated_fields
+ invalidate_prices_updated_fields
+ + shipping_update_fields
)
call_checkout_info_event(
diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.py
index e204b48d34..13d985804b 100644
--- a/saleor/graphql/checkout/mutations/utils.py
+++ b/saleor/graphql/checkout/mutations/utils.py
@@ -112,12 +112,17 @@ def update_checkout_external_shipping_method_if_invalid(
def update_checkout_shipping_method_if_invalid(
checkout_info: "CheckoutInfo", lines: list[CheckoutLineInfo]
-):
+) -> list[str]:
+ """Check if current shipping method is valid and clean it if not.
+
+ The method is not saving the applied changes on checkout.
+ """
quantity = calculate_checkout_quantity(lines)
+ update_fields = []
# remove shipping method when empty checkout
if quantity == 0 or not is_shipping_required(lines):
- clear_delivery_method(checkout_info)
+ update_fields = clear_delivery_method(checkout_info)
is_valid = clean_delivery_method(
checkout_info=checkout_info,
@@ -125,7 +130,9 @@ def update_checkout_shipping_method_if_invalid(
)
if not is_valid:
- clear_delivery_method(checkout_info)
+ update_fields = clear_delivery_method(checkout_info)
+
+ return update_fields
def get_variants_and_total_quantities(
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
index 3305842c9a..e3d9f69d43 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
@@ -1323,3 +1323,43 @@ def test_checkout_shipping_address_update_change_save_address_option_to_true(
assert_address_data(checkout.shipping_address, graphql_address_data)
assert checkout.save_shipping_address is True
assert checkout.save_billing_address is False
+
+
+def test_checkout_shipping_address_update_when_switching_from_cc(
+ checkout_with_items,
+ app_api_client,
+ graphql_address_data_skipped_validation,
+ permission_handle_checkouts,
+ shipping_method,
+ address,
+):
+ # given
+ checkout = checkout_with_items
+ address_data = graphql_address_data_skipped_validation
+ # after switching for cc to standard shipping method - the shipping method is set
+ # and the shipping address is cleared
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.shipping_address = None
+ checkout.save(
+ update_fields=["shipping_method", "billing_address", "shipping_address"]
+ )
+
+ variables = {
+ "id": to_global_id_or_none(checkout_with_items),
+ "shippingAddress": address_data,
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
+ variables,
+ permissions=[permission_handle_checkouts],
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["checkoutShippingAddressUpdate"]
+ assert not data["errors"]
+ checkout.refresh_from_db()
+ assert checkout.shipping_address
diff --git a/saleor/graphql/checkout/tests/test_checkout_price_expiration.py b/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
index 43335c4ea2..f7ab4bc927 100644
--- a/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
+++ b/saleor/graphql/checkout/tests/test_checkout_price_expiration.py
@@ -25,13 +25,14 @@ mutation addCheckoutLines($checkoutId: ID!, $line: CheckoutLineInput!) {
@patch("saleor.graphql.checkout.mutations.checkout_lines_add.invalidate_checkout")
def test_checkout_lines_add_invalidate_prices(
- mocked_function,
+ mocked_invalidate_checkout,
api_client,
checkout_with_items,
stock,
):
# given
manager = get_plugins_manager(allow_replica=False)
+ mocked_invalidate_checkout.return_value = []
query = ADD_CHECKOUT_LINES
variables = {
"checkoutId": graphene.Node.to_global_id("Checkout", checkout_with_items.pk),
@@ -51,7 +52,9 @@ def test_checkout_lines_add_invalidate_prices(
checkout_with_items.refresh_from_db()
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
- mocked_function.assert_called_once_with(checkout_info, lines, mock.ANY, save=True)
+ mocked_invalidate_checkout.assert_called_once_with(
+ checkout_info, lines, mock.ANY, save=False
+ )
UPDATE_CHECKOUT_LINES = """
@@ -68,13 +71,15 @@ mutation updateCheckoutLines($token: UUID!, $line: CheckoutLineUpdateInput!) {
@patch("saleor.graphql.checkout.mutations.checkout_lines_add.invalidate_checkout")
def test_checkout_lines_update_invalidate_prices(
- mocked_function,
+ mocked_invalidate_checkout,
api_client,
checkout_with_items,
stock,
):
# given
manager = get_plugins_manager(allow_replica=False)
+ mocked_invalidate_checkout.return_value = []
+
query = UPDATE_CHECKOUT_LINES
variables = {
"token": checkout_with_items.token,
@@ -87,14 +92,17 @@ def test_checkout_lines_update_invalidate_prices(
}
# when
- response = get_graphql_content(api_client.post_graphql(query, variables))
+ content = api_client.post_graphql(query, variables)
+ response = get_graphql_content(content)
# then
assert not response["data"]["checkoutLinesUpdate"]["errors"]
checkout_with_items.refresh_from_db()
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
- mocked_function.assert_called_once_with(checkout_info, lines, mock.ANY, save=True)
+ mocked_invalidate_checkout.assert_called_once_with(
+ checkout_info, lines, mock.ANY, save=False
+ )
DELETE_CHECKOUT_LINES = """
@@ -111,12 +119,13 @@ mutation deleteCheckoutLines($token: UUID!, $lineId: ID!){
@patch("saleor.graphql.checkout.mutations.checkout_lines_delete.invalidate_checkout")
def test_checkout_lines_delete_invalidate_prices(
- mocked_function,
+ mocked_invalidate_checkout,
api_client,
checkout_with_items,
):
# given
manager = get_plugins_manager(allow_replica=False)
+ mocked_invalidate_checkout.return_value = []
query = DELETE_CHECKOUT_LINES
variables = {
"token": checkout_with_items.token,
@@ -133,7 +142,9 @@ def test_checkout_lines_delete_invalidate_prices(
checkout_with_items.refresh_from_db()
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
- mocked_function.assert_called_once_with(checkout_info, lines, mock.ANY, save=True)
+ mocked_invalidate_checkout.assert_called_once_with(
+ checkout_info, lines, mock.ANY, save=False
+ )
DELETE_CHECKOUT_LINE = """
@@ -150,12 +161,13 @@ mutation deleteCheckoutLine($token: UUID!, $lineId: ID!){
@patch("saleor.graphql.checkout.mutations.checkout_line_delete.invalidate_checkout")
def test_checkout_line_delete_invalidate_prices(
- mocked_function,
+ mocked_invalidate_checkout,
api_client,
checkout_with_items,
):
# given
manager = get_plugins_manager(allow_replica=False)
+ mocked_invalidate_checkout.return_value = []
query = DELETE_CHECKOUT_LINE
variables = {
"token": checkout_with_items.token,
@@ -172,7 +184,9 @@ def test_checkout_line_delete_invalidate_prices(
checkout_with_items.refresh_from_db()
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
- mocked_function.assert_called_once_with(checkout_info, lines, mock.ANY, save=True)
+ mocked_invalidate_checkout.assert_called_once_with(
+ checkout_info, lines, mock.ANY, save=False
+ )
UPDATE_CHECKOUT_SHIPPING_ADDRESS = """
@@ -192,7 +206,7 @@ mutation UpdateCheckoutShippingAddress($token: UUID!, $address: AddressInput!) {
".invalidate_checkout"
)
def test_checkout_shipping_address_update_invalidate_prices(
- mocked_function,
+ mocked_invalidate_checkout,
api_client,
checkout_with_items,
graphql_address_data,
@@ -200,12 +214,13 @@ def test_checkout_shipping_address_update_invalidate_prices(
):
# given
manager = get_plugins_manager(allow_replica=False)
+ mocked_invalidate_checkout.return_value = []
query = UPDATE_CHECKOUT_SHIPPING_ADDRESS
variables = {
"token": checkout_with_items.token,
"address": graphql_address_data,
}
- mocked_function.return_value = []
+ mocked_invalidate_checkout.return_value = []
# when
response = get_graphql_content(api_client.post_graphql(query, variables))
@@ -215,7 +230,9 @@ def test_checkout_shipping_address_update_invalidate_prices(
checkout_with_items.refresh_from_db()
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
- mocked_function.assert_called_once_with(checkout_info, lines, mock.ANY, save=False)
+ mocked_invalidate_checkout.assert_called_once_with(
+ checkout_info, lines, mock.ANY, save=False
+ )
UPDATE_CHECKOUT_BILLING_ADDRESS = """
@@ -330,6 +347,7 @@ def test_checkout_delivery_method_update_invalidate_prices(
checkout = checkout_with_shipping_address_for_cc
checkout.price_expiration = timezone.now()
checkout.save(update_fields=["price_expiration"])
+
query = UPDATE_CHECKOUT_DELIVERY_METHOD
variables = {
"token": checkout.token,
diff --git a/saleor/tests/e2e/__init__.py b/saleor/tests/e2e/__init__.py
index 4d0ee9d4e1..4f87c176c3 100644
--- a/saleor/tests/e2e/__init__.py
+++ b/saleor/tests/e2e/__init__.py
@@ -15,13 +15,13 @@ DEFAULT_WAREHOUSE_ADDRESS = {
"firstName": "Saleor",
"lastName": "Mirumee",
"companyName": "Mirumee Software",
- "streetAddress1": "Tęczowa 7",
+ "streetAddress1": "2137 Cantebury Drive",
"streetAddress2": "",
- "postalCode": "53-601",
- "country": "PL",
- "city": "Wrocław",
- "countryArea": "",
- "phone": "+48321321888",
+ "postalCode": "10013",
+ "country": "US",
+ "city": "New York",
+ "countryArea": "NY",
+ "phone": "+19179920814",
}
ADDRESS_DE = {
diff --git a/saleor/tests/e2e/checkout/test_checkout_switch_from_cc_to_standard_delivery_method.py b/saleor/tests/e2e/checkout/test_checkout_switch_from_cc_to_standard_delivery_method.py
new file mode 100644
index 0000000000..25ae06c96e
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_checkout_switch_from_cc_to_standard_delivery_method.py
@@ -0,0 +1,119 @@
+import pytest
+
+from ..product.utils.preparing_product import prepare_product
+from ..shop.utils.preparing_shop import prepare_default_shop
+from ..utils import assign_permissions
+from ..warehouse.utils import update_warehouse
+from .utils import (
+ checkout_complete,
+ checkout_create,
+ checkout_delivery_method_update,
+ checkout_dummy_payment_create,
+ checkout_shipping_address_update,
+)
+
+
+def test_checkout_switch_from_cc_to_standard_delivery_method_CORE_0133(
+ e2e_staff_api_client,
+ e2e_logged_api_client,
+ permission_manage_product_types_and_attributes,
+ shop_permissions,
+):
+ # Before
+ permissions = [
+ permission_manage_product_types_and_attributes,
+ *shop_permissions,
+ ]
+
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+ update_warehouse(
+ e2e_staff_api_client,
+ warehouse_id,
+ is_private=False,
+ click_and_collect_option="LOCAL",
+ )
+ variant_price = 10
+
+ (
+ _product_id,
+ product_variant_id,
+ _product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price,
+ )
+
+ # Step 1 - Create checkout.
+ lines = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_create(
+ e2e_logged_api_client,
+ lines,
+ channel_slug,
+ email=None,
+ )
+ checkout_id = checkout_data["id"]
+
+ expected_email = e2e_logged_api_client.user.email
+ assert checkout_data["email"] == expected_email
+ assert checkout_data["user"]["email"] == expected_email
+ assert checkout_data["isShippingRequired"] is True
+ checkout_shipping_address = checkout_data["shippingAddress"]
+
+ collection_point = checkout_data["availableCollectionPoints"][0]
+ assert collection_point["id"] == warehouse_id
+ assert collection_point["isPrivate"] is False
+ assert collection_point["clickAndCollectOption"] == "LOCAL"
+
+ # Step 2 - Assign CC as a delivery method
+ checkout_data = checkout_delivery_method_update(
+ e2e_logged_api_client,
+ checkout_id,
+ collection_point["id"],
+ )
+ assert checkout_data["deliveryMethod"]["id"] == warehouse_id
+ # Ensure the address has been changed
+ assert (
+ checkout_data["shippingAddress"]["streetAddress1"]
+ != checkout_shipping_address["streetAddress1"]
+ )
+
+ # Step 3 - Change the delivery method to standard shipping method
+ checkout_data = checkout_delivery_method_update(
+ e2e_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ assert checkout_data["shippingAddress"] is None
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+
+ # Step 4 - Update shipping address
+ checkout_data = checkout_shipping_address_update(e2e_logged_api_client, checkout_id)
+ assert checkout_data["shippingAddress"]
+
+ # Step 5 - Create payment
+ checkout_dummy_payment_create(
+ e2e_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ )
+
+ # Step 6 - Complete the checkout
+ order_data = checkout_complete(
+ e2e_logged_api_client,
+ checkout_id,
+ )
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == total_gross_amount
+ assert order_data["deliveryMethod"]["id"] == shipping_method_id
diff --git a/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py b/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
index 9564bbaa7e..f88ae77c73 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_delivery_method_update.py
@@ -50,6 +50,9 @@ mutation checkoutDeliveryMethodUpdate($checkoutId: ID!, $deliveryMethodId: ID) {
amount
}
}
+ shippingAddress {
+ ...Address
+ }
deliveryMethod {
... on ShippingMethod {
id
diff --git a/saleor/tests/e2e/warehouse/utils/create_warehouse.py b/saleor/tests/e2e/warehouse/utils/create_warehouse.py
index 1fde92cc3f..f58992e8db 100644
--- a/saleor/tests/e2e/warehouse/utils/create_warehouse.py
+++ b/saleor/tests/e2e/warehouse/utils/create_warehouse.py
@@ -1,6 +1,6 @@
import uuid
-from ... import DEFAULT_ADDRESS
+from ... import DEFAULT_WAREHOUSE_ADDRESS
from ...utils import get_graphql_content
WAREHOUSE_CREATE_MUTATION = """
@@ -37,7 +37,7 @@ def create_warehouse(
staff_api_client,
name="Test warehouse",
slug=None,
- address=DEFAULT_ADDRESS,
+ address=DEFAULT_WAREHOUSE_ADDRESS,
):
if slug is None:
slug = f"warehouse_slug_{uuid.uuid4()}"