Skip to content

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

Make shipping / billing address and method required on order complete (#6285)

Tomasz Szymański0b966678ccc6
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index ec4da8367a..662af19fb7 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -988,7 +988,8 @@ def test_validate_draft_order_wrong_shipping(draft_order):
assert e.value.error_dict["shipping"][0].message == msg
-def test_validate_draft_order_no_order_lines(order):
+def test_validate_draft_order_no_order_lines(order, shipping_method):
+ order.shipping_method = shipping_method
with pytest.raises(ValidationError) as e:
validate_draft_order(order, "US")
msg = "Could not create order without any products."
@@ -1077,6 +1078,60 @@ def test_validate_draft_order_out_of_stock_variant(draft_order):
assert e.value.error_dict["lines"][0].message == msg
+def test_validate_draft_order_no_shipping_address(draft_order):
+ order = draft_order
+ order.shipping_address = None
+
+ with pytest.raises(ValidationError) as e:
+ validate_draft_order(order, "US")
+ error = e.value.error_dict["order"][0]
+ assert error.message == "Can't finalize draft with no shipping address."
+ assert error.code.value == OrderErrorCode.ORDER_NO_SHIPPING_ADDRESS.value
+
+
+def test_validate_draft_order_no_billing_address(draft_order):
+ order = draft_order
+ order.billing_address = None
+
+ with pytest.raises(ValidationError) as e:
+ validate_draft_order(order, "US")
+ error = e.value.error_dict["order"][0]
+ assert error.message == "Can't finalize draft with no billing address."
+ assert error.code.value == OrderErrorCode.BILLING_ADDRESS_NOT_SET.value
+
+
+def test_validate_draft_order_no_shipping_method(draft_order):
+ order = draft_order
+ order.shipping_method = None
+
+ with pytest.raises(ValidationError) as e:
+ validate_draft_order(order, "US")
+ error = e.value.error_dict["shipping"][0]
+ assert error.message == "Shipping method is required."
+ assert error.code.value == OrderErrorCode.SHIPPING_METHOD_REQUIRED.value
+
+
+def test_validate_draft_order_no_shipping_method_shipping_not_required(draft_order):
+ order = draft_order
+ order.shipping_method = None
+ required_mock = Mock(return_value=False)
+ order.is_shipping_required = required_mock
+
+ assert validate_draft_order(order, "US") is None
+
+
+def test_validate_draft_order_no_shipping_address_no_method_shipping_not_required(
+ draft_order,
+):
+ order = draft_order
+ order.shipping_method = None
+ order.shipping_address = None
+ required_mock = Mock(return_value=False)
+ order.is_shipping_required = required_mock
+
+ assert validate_draft_order(order, "US") is None
+
+
DRAFT_ORDER_COMPLETE_MUTATION = """
mutation draftComplete($id: ID!) {
draftOrderComplete(id: $id) {
@@ -1125,11 +1180,14 @@ def test_draft_order_complete(
def test_draft_order_complete_product_without_inventory_tracking(
staff_api_client,
+ shipping_method,
permission_manage_orders,
staff_user,
draft_order_without_inventory_tracking,
):
order = draft_order_without_inventory_tracking
+ order.shipping_method = shipping_method
+ order.save()
# Ensure no events were created
assert not OrderEvent.objects.exists()
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index fccaeeede3..1227dbc7e5 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -18,14 +18,19 @@ def validate_total_quantity(order):
def validate_shipping_method(order):
- method = order.shipping_method
- shipping_address = order.shipping_address
- shipping_not_valid = (
- method
- and shipping_address
- and shipping_address.country.code not in method.shipping_zone.countries
- ) # noqa
- if shipping_not_valid:
+ if not order.shipping_method:
+ raise ValidationError(
+ {
+ "shipping": ValidationError(
+ "Shipping method is required.",
+ code=OrderErrorCode.SHIPPING_METHOD_REQUIRED,
+ )
+ }
+ )
+ if (
+ order.shipping_address.country.code
+ not in order.shipping_method.shipping_zone.countries
+ ):
raise ValidationError(
{
"shipping": ValidationError(
@@ -36,6 +41,30 @@ def validate_shipping_method(order):
)
+def validate_billing_address(order):
+ if not order.billing_address:
+ raise ValidationError(
+ {
+ "order": ValidationError(
+ "Can't finalize draft with no billing address.",
+ code=OrderErrorCode.BILLING_ADDRESS_NOT_SET,
+ )
+ }
+ )
+
+
+def validate_shipping_address(order):
+ if not order.shipping_address:
+ raise ValidationError(
+ {
+ "order": ValidationError(
+ "Can't finalize draft with no shipping address.",
+ code=OrderErrorCode.ORDER_NO_SHIPPING_ADDRESS,
+ )
+ }
+ )
+
+
def validate_order_lines(order, country):
for line in order:
if line.variant is None:
@@ -98,7 +127,9 @@ def validate_draft_order(order, country):
Returns a list of errors if any were found.
"""
+ validate_billing_address(order)
if order.is_shipping_required():
+ validate_shipping_address(order)
validate_shipping_method(order)
validate_total_quantity(order)
validate_order_lines(order, country)