Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Deactivate single use voucher when completing draft order (#18698)
Iga Karbowiakd8ed4bedaea5
diff --git a/saleor/graphql/order/mutations/draft_order_complete.py b/saleor/graphql/order/mutations/draft_order_complete.pyindex 2acf39f27a..4ea83a479b 100644--- a/saleor/graphql/order/mutations/draft_order_complete.py+++ b/saleor/graphql/order/mutations/draft_order_complete.py@@ -85,6 +85,12 @@ class DraftOrderComplete(BaseMutation):)return order+ @classmethod+ def handle_order_voucher(cls, order, channel):+ if order.voucher:+ cls.setup_voucher_customer(order, channel)+ cls.deactivate_single_use_voucher_codes(order)+@classmethoddef setup_voucher_customer(cls, order, channel):if (@@ -99,6 +105,17 @@ class DraftOrderComplete(BaseMutation):code, get_customer_email_for_voucher_usage(order))+ @classmethod+ def deactivate_single_use_voucher_codes(cls, order):+ # In case the `include_draft_order_in_voucher_usage` flag is set to True,+ # the voucher is not deactivated during assigning it to the draft order.+ # So we need to deactivate it when the draft order is completed.+ if order.voucher.single_use and order.voucher_code:+ code = VoucherCode.objects.filter(code=order.voucher_code).first()+ if code and code.is_active:+ code.is_active = False+ code.save(update_fields=["is_active"])+@classmethoddef perform_mutation( # type: ignore[override]cls, _root, info: ResolveInfo, /, *, id: str@@ -174,7 +191,7 @@ class DraftOrderComplete(BaseMutation):update_order_display_gross_prices(order)order.save(update_fields=update_fields)- cls.setup_voucher_customer(order, channel)+ cls.handle_order_voucher(order, channel)order_lines_info = []lines = order.lines.all()for line in lines:diff --git a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py b/saleor/graphql/order/tests/mutations/test_draft_order_complete.pyindex 00f83596dd..55f9f11590 100644--- a/saleor/graphql/order/tests/mutations/test_draft_order_complete.py+++ b/saleor/graphql/order/tests/mutations/test_draft_order_complete.py@@ -3,6 +3,7 @@ from decimal import Decimalfrom unittest.mock import ANY, call, patchimport graphene+import pytestfrom django.db.models import Sumfrom django.test import override_settingsfrom django.utils import timezone@@ -1964,3 +1965,55 @@ def test_draft_order_complete_clear_line_draft_base_price_expire_at_field(for line in order.lines.all():assert line.draft_base_price_expire_at is None++[email protected]("include_draft_order_in_voucher_usage", [True, False])[email protected]("code_is_active", [True, False])+def test_draft_order_complete_with_single_use_voucher(+ staff_api_client,+ permission_group_manage_orders,+ draft_order,+ voucher,+ channel_USD,+ include_draft_order_in_voucher_usage,+ code_is_active,+):+ # given+ permission_group_manage_orders.user_set.add(staff_api_client.user)+ order = draft_order++ channel_USD.include_draft_order_in_voucher_usage = (+ include_draft_order_in_voucher_usage+ )+ channel_USD.save(update_fields=["include_draft_order_in_voucher_usage"])++ voucher.single_use = True+ voucher.save(update_fields=["single_use"])++ code_instance = voucher.codes.first()+ code_instance.is_active = code_is_active+ code_instance.save(update_fields=["is_active"])++ order.voucher = voucher+ order.voucher_code = code_instance.code+ order.should_refresh_prices = True+ order.save(update_fields=["voucher", "voucher_code", "should_refresh_prices"])+ create_or_update_voucher_discount_objects_for_order(order)++ 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)++ # then+ content = get_graphql_content(response)+ data = content["data"]["draftOrderComplete"]["order"]+ order.refresh_from_db()++ assert data["status"] == order.status.upper()+ assert data["voucherCode"] == code_instance.code+ assert data["voucher"]["code"] == voucher.code++ code_instance.refresh_from_db()+ assert not code_instance.is_active