Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Prevent finalizing draft orders with unpublished product
Damian W96401ca80d10
diff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.pyindex bff7e0f5b4..4ce1476f9f 100644--- a/saleor/graphql/order/utils.py+++ b/saleor/graphql/order/utils.py@@ -61,6 +61,19 @@ def validate_order_lines(order, country):)+def validate_product_is_published(order):+ for line in order:+ if not line.variant.product.is_published:+ raise ValidationError(+ {+ "lines": ValidationError(+ "Can't finalize draft with unpublished product.",+ code=OrderErrorCode.PRODUCT_NOT_PUBLISHED,+ )+ }+ )++def validate_draft_order(order, country):"""Check if the given order contains the proper data.@@ -68,6 +81,7 @@ def validate_draft_order(order, country):- Shipping address and method are set up,- Product variants for order lines still exists in database.- Product variants are availale in requested quantity.+ - Product variants are published.Returns a list of errors if any were found."""@@ -75,3 +89,4 @@ def validate_draft_order(order, country):validate_shipping_method(order)validate_total_quantity(order)validate_order_lines(order, country)+ validate_product_is_published(order)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 887cde4e93..0e75b90d80 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2672,6 +2672,7 @@ enum OrderErrorCode {FULFILL_ORDER_LINEGRAPHQL_ERRORINVALID+ PRODUCT_NOT_PUBLISHEDNOT_FOUNDORDER_NO_SHIPPING_ADDRESSPAYMENT_ERRORdiff --git a/saleor/order/error_codes.py b/saleor/order/error_codes.pyindex e16f113cc9..22bebc1723 100644--- a/saleor/order/error_codes.py+++ b/saleor/order/error_codes.py@@ -12,6 +12,7 @@ class OrderErrorCode(Enum):FULFILL_ORDER_LINE = "fulfill_order_line"GRAPHQL_ERROR = "graphql_error"INVALID = "invalid"+ PRODUCT_NOT_PUBLISHED = "product_not_published"NOT_FOUND = "not_found"ORDER_NO_SHIPPING_ADDRESS = "order_no_shipping_address"PAYMENT_ERROR = "payment_error"diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 562664906d..8e8f07e920 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -785,6 +785,23 @@ def test_validate_draft_order_non_existing_variant(draft_order):assert e.value.error_dict["lines"][0].message == msg+def test_validate_draft_order_with_unpublished_product(draft_order):+ order = draft_order+ line = order.lines.first()+ variant = line.variant+ variant.product.is_published = False+ variant.product.save()+ line.refresh_from_db()++ with pytest.raises(ValidationError) as e:+ validate_draft_order(order, "US")+ msg = "Can't finalize draft with unpublished product."+ error = e.value.error_dict["lines"][0]++ assert error.message == msg+ assert error.code == OrderErrorCode.PRODUCT_NOT_PUBLISHED++def test_validate_draft_order_out_of_stock_variant(draft_order):order = draft_orderline = order.lines.first()