Skip to content

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

Do not allow finalizing draft order with shipping method not available in order channel

IKarbowiak8b92f110590a
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index f9bd830c3c..95e3fcd7bc 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -2187,6 +2187,18 @@ def test_can_finalize_order(staff_api_client, permission_manage_orders, draft_or
assert content["data"]["order"]["canFinalize"] is True
+def test_can_finalize_order_invalid_shipping_method_set(
+ staff_api_client, permission_manage_orders, draft_order
+):
+ order_id = graphene.Node.to_global_id("Order", draft_order.id)
+ draft_order.channel.shipping_zones.clear()
+ variables = {"id": order_id}
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_CAN_FINALIZE_QUERY, variables)
+ content = get_graphql_content(response)
+ assert content["data"]["order"]["canFinalize"] is False
+
+
def test_can_finalize_order_no_order_lines(
staff_api_client, permission_manage_orders, order
):
@@ -2515,8 +2527,11 @@ def test_draft_order_complete_channel_without_shipping_zones(
content = get_graphql_content(response)
data = content["data"]["draftOrderComplete"]
assert len(data["orderErrors"]) == 1
- assert data["orderErrors"][0]["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
- assert data["orderErrors"][0]["field"] == "lines"
+ assert (
+ data["orderErrors"][0]["code"]
+ == OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name
+ )
+ assert data["orderErrors"][0]["field"] == "shipping"
def test_draft_order_complete_product_without_inventory_tracking(
@@ -2566,6 +2581,43 @@ def test_draft_order_complete_product_without_inventory_tracking(
assert not OrderEvent.objects.exclude(**event_params).exists()
+def test_draft_order_complete_not_available_shipping_method(
+ staff_api_client,
+ permission_manage_orders,
+ staff_user,
+ draft_order,
+):
+ # given
+ order = draft_order
+ order.channel.shipping_zones.clear()
+
+ # Ensure no events were created
+ assert not OrderEvent.objects.exists()
+
+ # Ensure no allocation were created
+ assert not Allocation.objects.filter(order_line__order=order).exists()
+
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ variables = {"id": order_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ DRAFT_ORDER_COMPLETE_MUTATION, variables, permissions=[permission_manage_orders]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderComplete"]["order"]
+ content = get_graphql_content(response)
+ data = content["data"]["draftOrderComplete"]
+ assert len(data["orderErrors"]) == 1
+ assert (
+ data["orderErrors"][0]["code"]
+ == OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name
+ )
+ assert data["orderErrors"][0]["field"] == "shipping"
+
+
def test_draft_order_complete_out_of_stock_variant(
staff_api_client, permission_manage_orders, staff_user, draft_order
):
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.py
index 4384a9f452..9d42b4ce4b 100644
--- a/saleor/graphql/order/utils.py
+++ b/saleor/graphql/order/utils.py
@@ -41,6 +41,15 @@ def validate_shipping_method(order):
)
}
)
+ if not order.shipping_method.shipping_zone.channels.filter(id=order.channel_id):
+ raise ValidationError(
+ {
+ "shipping": ValidationError(
+ "Shipping method not available in given channel.",
+ code=OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE,
+ )
+ }
+ )
def validate_billing_address(order):