Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Draft order shipping method doesn't update after removed all products. (#7799)
Dawid Matuszczykcd455722fed7
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex b38dd79e79..2f4fde3ae3 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -220,7 +220,9 @@ class OrderUpdate(DraftOrderCreate):class OrderUpdateShippingInput(graphene.InputObjectType):shipping_method = graphene.ID(- description="ID of the selected shipping method.", name="shippingMethod"+ description="ID of the selected shipping method,"+ " pass null to remove currently assigned shipping method.",+ name="shippingMethod",)@@ -251,11 +253,16 @@ class OrderUpdateShipping(EditableOrderValidationMixin, BaseMutation):description="ID of the order to update a shipping method.",)input = OrderUpdateShippingInput(- description="Fields required to change shipping method of the order."+ description="Fields required to change shipping method of the order.",+ required=True,)class Meta:- description = "Updates a shipping method of the order."+ description = (+ "Updates a shipping method of the order."+ " Requires shipping method ID to update, when null is passed "+ "then currently assigned shipping method is removed."+ )permissions = (OrderPermissions.MANAGE_ORDERS,)error_type_class = OrderErrorerror_type_field = "order_errors"@@ -269,9 +276,20 @@ class OrderUpdateShipping(EditableOrderValidationMixin, BaseMutation):qs=models.Order.objects.prefetch_related("lines"),)cls.validate_order(order)+data = data.get("input")- if not data["shipping_method"]:+ if "shipping_method" not in data:+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method must be provided to perform mutation.",+ code=OrderErrorCode.SHIPPING_METHOD_REQUIRED,+ )+ }+ )++ if not data.get("shipping_method"):if not order.is_draft() and order.is_shipping_required():raise ValidationError({@@ -282,6 +300,17 @@ class OrderUpdateShipping(EditableOrderValidationMixin, BaseMutation):})+ # Shipping method is detached only when null is passed in input.+ if data["shipping_method"] == "":+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method cannot be empty.",+ code=OrderErrorCode.SHIPPING_METHOD_REQUIRED,+ )+ }+ )+order.shipping_method = Noneorder.shipping_price = zero_taxed_money(order.currency)order.shipping_method_name = None@@ -294,6 +323,7 @@ class OrderUpdateShipping(EditableOrderValidationMixin, BaseMutation):"shipping_method_name",])+ recalculate_order(order)return OrderUpdateShipping(order=order)method = cls.get_node_or_error(@@ -824,6 +854,19 @@ class OrderLineDelete(EditableOrderValidationMixin, BaseMutation):delete_order_line(line_info)line.id = db_id+ if not order.is_shipping_required():+ order.shipping_method = None+ order.shipping_price = zero_taxed_money(order.currency)+ order.shipping_method_name = None+ order.save(+ update_fields=[+ "currency",+ "shipping_method",+ "shipping_price_net_amount",+ "shipping_price_gross_amount",+ "shipping_method_name",+ ]+ )# Create the removal eventevents.order_removed_products_event(order=order,diff --git a/saleor/graphql/order/schema.py b/saleor/graphql/order/schema.pyindex 00bcd06b63..e38a263a1c 100644--- a/saleor/graphql/order/schema.py+++ b/saleor/graphql/order/schema.py@@ -139,7 +139,9 @@ class OrderMutations(graphene.ObjectType):draft_order_create = DraftOrderCreate.Field()draft_order_delete = DraftOrderDelete.Field()draft_order_bulk_delete = DraftOrderBulkDelete.Field()- draft_order_lines_bulk_delete = DraftOrderLinesBulkDelete.Field()+ draft_order_lines_bulk_delete = DraftOrderLinesBulkDelete.Field(+ deprecation_reason="DEPRECATED: Will be removed in Saleor 4.0."+ )draft_order_update = DraftOrderUpdate.Field()order_add_note = OrderAddNote.Field()diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 37add1d793..333e0df60f 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -3701,6 +3701,12 @@ ORDER_LINE_DELETE_MUTATION = """}order {id+ total{+ gross{+ currency+ amount+ }+ }}}}@@ -6483,3 +6489,95 @@ def test_get_variant_from_order_line_variant_not_exists_as_staff(content = get_graphql_content(response)orders = content["data"]["me"]["orders"]["edges"]assert orders[0]["node"]["lines"][0]["variant"] is None+++def test_draft_order_properly_recalculate_total_after_shipping_product_removed(+ staff_api_client,+ draft_order,+ permission_manage_orders,+):+ order = draft_order+ line = order.lines.get(product_sku="SKU_AA")+ line.is_shipping_required = True+ line.save()++ query = ORDER_LINE_DELETE_MUTATION+ line = order.lines.get(product_sku="SKU_B")+ line_id = graphene.Node.to_global_id("OrderLine", line.id)+ variables = {"id": line_id}++ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ data = content["data"]["orderLineDelete"]++ assert data["order"]["total"]["gross"]["amount"] == float(+ order.total_gross_amount+ ) - float(line.total_price_gross_amount)+++ORDER_UPDATE_SHIPPING_QUERY_WITH_TOTAL = """+mutation OrderUpdateShipping(+ $orderId: ID!+ $shippingMethod: OrderUpdateShippingInput!+) {+ orderUpdateShipping(order: $orderId, input: $shippingMethod) {+ order {+ shippingMethod {+ id+ }+ }+ errors {+ field+ message+ }+ }+}+"""+++ "input, response_msg",+ [+ ({"shippingMethod": ""}, "Shipping method cannot be empty."),+ ({}, "Shipping method must be provided to perform mutation."),+ ],+)+def test_order_shipping_update_mutation_return_error_for_empty_value(+ draft_order, permission_manage_orders, staff_api_client, input, response_msg+):+ query = ORDER_UPDATE_SHIPPING_QUERY_WITH_TOTAL++ order = draft_order+ order_id = graphene.Node.to_global_id("Order", order.id)+ variables = {"orderId": order_id, "shippingMethod": input}+ response = staff_api_client.post_graphql(+ query,+ variables,+ permissions=(permission_manage_orders,),+ )+ content = get_graphql_content(response)+ data = content["data"]["orderUpdateShipping"]++ assert data["errors"][0]["message"] == response_msg+++def test_order_shipping_update_mutation_properly_recalculate_total(+ draft_order,+ permission_manage_orders,+ staff_api_client,+):+ query = ORDER_UPDATE_SHIPPING_QUERY_WITH_TOTAL++ order = draft_order+ order_id = graphene.Node.to_global_id("Order", order.id)+ variables = {"orderId": order_id, "shippingMethod": {"shippingMethod": None}}+ response = staff_api_client.post_graphql(+ query,+ variables,+ permissions=(permission_manage_orders,),+ )+ content = get_graphql_content(response)+ data = content["data"]["orderUpdateShipping"]+ assert data["order"]["shippingMethod"] is Nonediff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex ef2c6c3ad5..4b81d806be 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2820,7 +2820,7 @@ type Mutation {draftOrderCreate(input: DraftOrderCreateInput!): DraftOrderCreatedraftOrderDelete(id: ID!): DraftOrderDeletedraftOrderBulkDelete(ids: [ID]!): DraftOrderBulkDelete- draftOrderLinesBulkDelete(ids: [ID]!): DraftOrderLinesBulkDelete+ draftOrderLinesBulkDelete(ids: [ID]!): DraftOrderLinesBulkDelete @deprecated(reason: "DEPRECATED: Will be removed in Saleor 4.0.")draftOrderUpdate(id: ID!, input: DraftOrderInput!): DraftOrderUpdateorderAddNote(order: ID!, input: OrderAddNoteInput!): OrderAddNoteorderCancel(id: ID!): OrderCancel@@ -2842,7 +2842,7 @@ type Mutation {orderMarkAsPaid(id: ID!, transactionReference: String): OrderMarkAsPaidorderRefund(amount: PositiveDecimal!, id: ID!): OrderRefundorderUpdate(id: ID!, input: OrderUpdateInput!): OrderUpdate- orderUpdateShipping(order: ID!, input: OrderUpdateShippingInput): OrderUpdateShipping+ orderUpdateShipping(order: ID!, input: OrderUpdateShippingInput!): OrderUpdateShippingorderVoid(id: ID!): OrderVoidorderBulkCancel(ids: [ID]!): OrderBulkCanceldeleteMetadata(id: ID!, keys: [String!]!): DeleteMetadata