Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix tax calculation for Click and Collect option. (#15505)
Artur22a1aa3ef0bc
diff --git a/saleor/checkout/error_codes.py b/saleor/checkout/error_codes.pyindex 9c9ccd9493..158bfc0884 100644--- a/saleor/checkout/error_codes.py+++ b/saleor/checkout/error_codes.py@@ -32,6 +32,7 @@ class CheckoutErrorCode(Enum):INACTIVE_PAYMENT = "inactive_payment"NON_EDITABLE_GIFT_LINE = "non_editable_gift_line"NON_REMOVABLE_GIFT_LINE = "non_removable_gift_line"+ SHIPPING_CHANGE_FORBIDDEN = "shipping_change_forbidden"class OrderCreateFromCheckoutErrorCode(Enum):diff --git a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.pyindex a52752cc0a..b07aae6049 100644--- a/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py+++ b/saleor/graphql/checkout/mutations/checkout_delivery_method_update.py@@ -21,6 +21,7 @@ from ....checkout.utils import (from ....shipping import interface as shipping_interfacefrom ....shipping import models as shipping_modelsfrom ....shipping.utils import convert_to_shipping_method_data+from ....warehouse import WarehouseClickAndCollectOptionfrom ....warehouse import models as warehouse_modelsfrom ....webhook.const import APP_ID_PREFIXfrom ....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType@@ -59,7 +60,9 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):class Meta:description = ("Updates the delivery method (shipping method or pick up point) "- "of the checkout." + ADDED_IN_31+ "of the checkout. "+ "Updates the checkout shipping_address for click and collect delivery "+ "for a warehouse address. " + ADDED_IN_31)doc_category = DOC_CATEGORY_CHECKOUTerror_type_class = CheckoutError@@ -238,6 +241,7 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):external_shipping_method: Optional[shipping_interface.ShippingMethodData],collection_point: Optional[Warehouse],) -> None:+ checkout_fields_to_update = ["shipping_method", "collection_point"]checkout = checkout_info.checkoutif external_shipping_method:set_external_shipping_id(@@ -247,15 +251,19 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):delete_external_shipping_id(checkout=checkout)checkout.shipping_method = shipping_methodcheckout.collection_point = collection_point+ if (+ collection_point is not None+ and collection_point.click_and_collect_option+ == WarehouseClickAndCollectOption.LOCAL_STOCK+ ):+ checkout.shipping_address = collection_point.address+ checkout_info.shipping_address = collection_point.address+ checkout_fields_to_update += ["shipping_address"]invalidate_prices_updated_fields = invalidate_checkout(checkout_info, lines, manager, save=False)checkout.save(- update_fields=[- "shipping_method",- "collection_point",- ]- + invalidate_prices_updated_fields+ update_fields=checkout_fields_to_update + invalidate_prices_updated_fields)get_or_create_checkout_metadata(checkout).save()cls.call_event(manager.checkout_updated, checkout)@@ -324,7 +332,6 @@ class CheckoutDeliveryMethodUpdate(BaseMutation):})type_name = cls._resolve_delivery_method_type(delivery_method_id)-checkout_info = fetch_checkout_info(checkout, lines, manager)if type_name == "Warehouse":return cls.perform_on_collection_point(diff --git a/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py b/saleor/graphql/checkout/mutations/checkout_shipping_address_update.pyindex 536ba83eac..028035eb84 100644--- a/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py+++ b/saleor/graphql/checkout/mutations/checkout_shipping_address_update.py@@ -33,6 +33,7 @@ from ...site.dataloaders import get_site_promisefrom ..types import Checkoutfrom .checkout_create import CheckoutAddressValidationRulesfrom .utils import (+ ERROR_CC_ADDRESS_CHANGE_FORBIDDEN,ERROR_DOES_NOT_SHIP,check_lines_quantity,get_checkout,@@ -153,6 +154,16 @@ class CheckoutShippingAddressUpdate(AddressMetadataMixin, BaseMutation, I18nMixi)})+ # prevent from changing the shipping address when click and collect is used.+ if checkout.collection_point_id:+ raise ValidationError(+ {+ "shipping_address": ValidationError(+ ERROR_CC_ADDRESS_CHANGE_FORBIDDEN,+ code=CheckoutErrorCode.SHIPPING_CHANGE_FORBIDDEN.value,+ )+ }+ )address_validation_rules = validation_rules or {}shipping_address_instance = cls.validate_address(shipping_address,diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.pyindex abd41b4991..3d1d3afa89 100644--- a/saleor/graphql/checkout/mutations/utils.py+++ b/saleor/graphql/checkout/mutations/utils.py@@ -46,6 +46,10 @@ if TYPE_CHECKING:ERROR_DOES_NOT_SHIP = "This checkout doesn't need shipping"+ERROR_CC_ADDRESS_CHANGE_FORBIDDEN = (+ "Can't change shipping address manually. "+ "For click and collect delivery, address is set to a warehouse address."+)@dataclassdiff --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 853f12c146..6a9f7f01f9 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@@ -447,7 +447,116 @@ def test_checkout_delivery_method_update_shipping_zone_with_channel(assert checkout.shipping_method == shipping_method[email protected]("is_valid_delivery_method", [True, False])+ ("delivery_method", "node_name", "attribute_name"),+ [+ ("shipping_method", "ShippingMethod", "shipping_method"),+ ],+ indirect=("delivery_method",),+)+@patch(+ "saleor.graphql.checkout.mutations.checkout_delivery_method_update."+ "clean_delivery_method"+)+def test_checkout_delivery_method_update_valid_method_not_all_shipping_data(+ mock_clean_delivery,+ api_client,+ delivery_method,+ node_name,+ attribute_name,+ checkout_with_item_for_cc,+):+ # given+ mock_clean_delivery.return_value = True++ checkout = checkout_with_item_for_cc+ checkout.shipping_address = Address.objects.create(country="US")+ checkout.save()+ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)+ shipping_method_data = convert_to_shipping_method_data(+ delivery_method,+ delivery_method.channel_listings.get(),+ )+ query = MUTATION_UPDATE_DELIVERY_METHOD+ mock_clean_delivery.return_value = True++ method_id = graphene.Node.to_global_id(node_name, delivery_method.id)++ # when+ response = api_client.post_graphql(+ query, {"id": to_global_id_or_none(checkout), "deliveryMethodId": method_id}+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ checkout.refresh_from_db()++ mock_clean_delivery.assert_called_once_with(+ checkout_info=checkout_info, lines=lines, method=shipping_method_data+ )+ errors = data["errors"]++ assert not errors+ assert getattr(checkout, attribute_name) == delivery_method+++ "delivery_method, node_name, attribute_name", # noqa: PT006+ [+ ("warehouse", "Warehouse", "collection_point"),+ ],+ indirect=("delivery_method",),+)+@patch(+ "saleor.graphql.checkout.mutations.checkout_delivery_method_update."+ "clean_delivery_method"+)+def test_checkout_delivery_method_update_valid_method_not_all_shipping_data_for_cc(+ mock_clean_delivery,+ api_client,+ delivery_method,+ node_name,+ attribute_name,+ checkout_with_item_for_cc,+ warehouse_for_cc,+):+ # given+ mock_clean_delivery.return_value = True+ checkout_address = Address.objects.create(country="US")+ checkout = checkout_with_item_for_cc+ checkout.shipping_address = checkout_address+ checkout.save()+ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ shipping_method_data = delivery_method+ checkout_info.shipping_address = shipping_method_data.address+ query = MUTATION_UPDATE_DELIVERY_METHOD+ mock_clean_delivery.return_value = True++ method_id = graphene.Node.to_global_id(node_name, delivery_method.id)++ # when+ response = api_client.post_graphql(+ query, {"id": to_global_id_or_none(checkout), "deliveryMethodId": method_id}+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ checkout.refresh_from_db()++ mock_clean_delivery.assert_called_once_with(+ checkout_info=checkout_info, lines=lines, method=shipping_method_data+ )+ errors = data["errors"]+ assert checkout.shipping_address == delivery_method.address+ assert not errors+ assert getattr(checkout, attribute_name) == delivery_method++@pytest.mark.parametrize(("delivery_method", "node_name", "attribute_name"),[@@ -460,17 +569,16 @@ def test_checkout_delivery_method_update_shipping_zone_with_channel("saleor.graphql.checkout.mutations.checkout_delivery_method_update.""clean_delivery_method")-def test_checkout_delivery_method_update_with_not_all_required_shipping_address_data(+def test_checkout_delivery_method_update_invalid_method_not_all_shipping_data(mock_clean_delivery,api_client,delivery_method,node_name,attribute_name,checkout_with_item_for_cc,- is_valid_delivery_method,):# given- mock_clean_delivery.return_value = is_valid_delivery_method+ mock_clean_delivery.return_value = Falsecheckout = checkout_with_item_for_cccheckout.shipping_address = Address.objects.create(country="US")@@ -486,7 +594,7 @@ def test_checkout_delivery_method_update_with_not_all_required_shipping_address_delivery_method.channel_listings.get(),)query = MUTATION_UPDATE_DELIVERY_METHOD- mock_clean_delivery.return_value = is_valid_delivery_method+ mock_clean_delivery.return_value = Falsemethod_id = graphene.Node.to_global_id(node_name, delivery_method.id)@@ -503,22 +611,16 @@ def test_checkout_delivery_method_update_with_not_all_required_shipping_address_checkout_info=checkout_info, lines=lines, method=shipping_method_data)errors = data["errors"]- if is_valid_delivery_method:- assert not errors- assert getattr(checkout, attribute_name) == delivery_method- else:- assert len(errors) == 1- assert errors[0]["field"] == "deliveryMethodId"- assert (- errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name- )- assert checkout.shipping_method is None- assert checkout.collection_point is None++ assert len(errors) == 1+ assert errors[0]["field"] == "deliveryMethodId"+ assert errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name+ assert checkout.shipping_method is None+ assert checkout.collection_point is None[email protected]("is_valid_delivery_method", [True, False])@pytest.mark.parametrize(- ("delivery_method", "node_name", "attribute_name"),+ "delivery_method, node_name, attribute_name", # noqa: PT006[("warehouse", "Warehouse", "collection_point"),("shipping_method", "ShippingMethod", "shipping_method"),@@ -529,17 +631,16 @@ def test_checkout_delivery_method_update_with_not_all_required_shipping_address_"saleor.graphql.checkout.mutations.checkout_delivery_method_update.""clean_delivery_method")-def test_checkout_delivery_method_update_with_not_valid_address_data(+def test_checkout_delivery_method_update_invalid_with_not_valid_address_data(mock_clean_delivery,api_client,delivery_method,node_name,attribute_name,checkout_with_item_for_cc,- is_valid_delivery_method,):# given- mock_clean_delivery.return_value = is_valid_delivery_method+ mock_clean_delivery.return_value = Falsecheckout = checkout_with_item_for_cccheckout.shipping_address = Address.objects.create(@@ -561,7 +662,7 @@ def test_checkout_delivery_method_update_with_not_valid_address_data(delivery_method.channel_listings.get(),)query = MUTATION_UPDATE_DELIVERY_METHOD- mock_clean_delivery.return_value = is_valid_delivery_method+ mock_clean_delivery.return_value = Falsemethod_id = graphene.Node.to_global_id(node_name, delivery_method.id)@@ -578,17 +679,134 @@ def test_checkout_delivery_method_update_with_not_valid_address_data(checkout_info=checkout_info, lines=lines, method=shipping_method_data)errors = data["errors"]- if is_valid_delivery_method:- assert not errors- assert getattr(checkout, attribute_name) == delivery_method- else:- assert len(errors) == 1- assert errors[0]["field"] == "deliveryMethodId"- assert (- errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name- )- assert checkout.shipping_method is None- assert checkout.collection_point is None++ assert len(errors) == 1+ assert errors[0]["field"] == "deliveryMethodId"+ assert errors[0]["code"] == CheckoutErrorCode.DELIVERY_METHOD_NOT_APPLICABLE.name+ assert checkout.shipping_method is None+ assert checkout.collection_point is None+++ "delivery_method, node_name, attribute_name", # noqa: PT006+ [+ ("shipping_method", "ShippingMethod", "shipping_method"),+ ],+ indirect=("delivery_method",),+)+@patch(+ "saleor.graphql.checkout.mutations.checkout_delivery_method_update."+ "clean_delivery_method"+)+def test_checkout_delivery_method_update_valid_with_not_valid_address_data(+ mock_clean_delivery,+ api_client,+ delivery_method,+ node_name,+ attribute_name,+ checkout_with_item_for_cc,+):+ # given+ mock_clean_delivery.return_value = True++ checkout = checkout_with_item_for_cc+ checkout.shipping_address = Address.objects.create(+ country="US",+ city="New York",+ city_area="ABC",+ street_address_1="New street",+ postal_code="53-601",+ )+ checkout.save()+ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ shipping_method_data = convert_to_shipping_method_data(+ delivery_method,+ delivery_method.channel_listings.get(),+ )+ query = MUTATION_UPDATE_DELIVERY_METHOD+ mock_clean_delivery.return_value = True++ method_id = graphene.Node.to_global_id(node_name, delivery_method.id)++ # when+ response = api_client.post_graphql(+ query, {"id": to_global_id_or_none(checkout), "deliveryMethodId": method_id}+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ checkout.refresh_from_db()++ mock_clean_delivery.assert_called_once_with(+ checkout_info=checkout_info, lines=lines, method=shipping_method_data+ )+ errors = data["errors"]++ assert not errors+ assert getattr(checkout, attribute_name) == delivery_method+++ "delivery_method, node_name, attribute_name", # noqa: PT006+ [+ ("warehouse", "Warehouse", "collection_point"),+ ],+ indirect=("delivery_method",),+)+@patch(+ "saleor.graphql.checkout.mutations.checkout_delivery_method_update."+ "clean_delivery_method"+)+def test_checkout_delivery_method_update_valid_with_not_valid_address_data_for_cc(+ mock_clean_delivery,+ api_client,+ delivery_method,+ node_name,+ attribute_name,+ checkout_with_item_for_cc,+):+ # given+ mock_clean_delivery.return_value = True++ checkout = checkout_with_item_for_cc+ checkout.shipping_address = Address.objects.create(+ country="US",+ city="New York",+ city_area="ABC",+ street_address_1="New street",+ postal_code="53-601",+ )+ checkout.save()+ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ shipping_method_data = delivery_method+ checkout_info.shipping_address = shipping_method_data.address+ query = MUTATION_UPDATE_DELIVERY_METHOD+ mock_clean_delivery.return_value = True++ method_id = graphene.Node.to_global_id(node_name, delivery_method.id)++ # when+ response = api_client.post_graphql(+ query, {"id": to_global_id_or_none(checkout), "deliveryMethodId": method_id}+ )++ # then+ data = get_graphql_content(response)["data"]["checkoutDeliveryMethodUpdate"]+ checkout.refresh_from_db()++ mock_clean_delivery.assert_called_once_with(+ checkout_info=checkout_info, lines=lines, method=shipping_method_data+ )+ errors = data["errors"]++ assert not errors+ assert getattr(checkout, attribute_name) == delivery_methoddef test_with_active_problems_flow(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.pyindex dace332377..ee3eadd1ca 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@@ -989,3 +989,29 @@ def test_with_active_problems_flow(# thenassert not content["data"]["checkoutShippingAddressUpdate"]["errors"]+++def test_checkout_shipping_address_update_with_collection_point_already_set(+ user_api_client,+ checkout_with_item,+ graphql_address_data,+ warehouse_for_cc,+):+ checkout = checkout_with_item+ checkout.collection_point_id = warehouse_for_cc.id+ checkout.save(update_fields=["collection_point_id"])++ shipping_address = graphql_address_data+ variables = {+ "id": to_global_id_or_none(checkout),+ "shippingAddress": shipping_address,+ }++ response = user_api_client.post_graphql(+ MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE, variables+ )+ content = get_graphql_content(response)+ data = content["data"]["checkoutShippingAddressUpdate"]+ errors = data["errors"]+ assert errors[0]["code"] == CheckoutErrorCode.SHIPPING_CHANGE_FORBIDDEN.name+ assert errors[0]["field"] == "shippingAddress"diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex e466409b03..1be8c0e71a 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -19056,7 +19056,7 @@ type Mutation {): CheckoutShippingMethodUpdate @doc(category: "Checkout") @webhookEventsInfo(asyncEvents: [CHECKOUT_UPDATED], syncEvents: [SHIPPING_LIST_METHODS_FOR_CHECKOUT]) @deprecated(reason: "This field will be removed in Saleor 4.0. Use `checkoutDeliveryMethodUpdate` instead.")"""- Updates the delivery method (shipping method or pick up point) of the checkout.+ Updates the delivery method (shipping method or pick up point) of the checkout. Updates the checkout shipping_address for click and collect delivery for a warehouse address.Added in Saleor 3.1.@@ -28897,6 +28897,7 @@ enum CheckoutErrorCode @doc(category: "Checkout") {INACTIVE_PAYMENTNON_EDITABLE_GIFT_LINENON_REMOVABLE_GIFT_LINE+ SHIPPING_CHANGE_FORBIDDEN}"""@@ -29333,7 +29334,7 @@ type CheckoutShippingMethodUpdate @doc(category: "Checkout") @webhookEventsInfo(}"""-Updates the delivery method (shipping method or pick up point) of the checkout.+Updates the delivery method (shipping method or pick up point) of the checkout. Updates the checkout shipping_address for click and collect delivery for a warehouse address.Added in Saleor 3.1.