Skip to content

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

Associate guest checkout with account if exists (#12758)

FremahA452cb8a3b4fb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 09da33df19..7c9377b77c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -48,7 +48,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `ORDER_REFUNDED`, `ORDER_FULLY_REFUNDED`, `ORDER_PAID` webhooks - #12533 by @korycins
- Handle error raised by 0Auth when fetching token - #12672 by @IKarbowiakg
- Fix adding new lines to draft order when existing line has deleted product - #12711 by @SzymJ
-- Remove `ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL` env variable from settings - ##12781 by @SzymJ
+- Upgrade checkout `complete_checkout` to assign guest checkout to account if exists - #12758 by @FremahA
+- Remove `ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL` env variable from settings - ##12781 by @Szym
# 3.13.0
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index d358983a3f..2e49f087ab 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -23,7 +23,7 @@ from prices import Money, TaxedMoney
from ..account.error_codes import AccountErrorCode
from ..account.models import User
-from ..account.utils import store_user_address
+from ..account.utils import retrieve_user_by_email, store_user_address
from ..channel import MarkAsPaidStrategy
from ..checkout import CheckoutAuthorizeStatus, calculations
from ..checkout.error_codes import CheckoutErrorCode
@@ -1275,6 +1275,14 @@ def complete_checkout(
metadata_list: Optional[List] = None,
private_metadata_list: Optional[List] = None,
) -> Tuple[Optional[Order], bool, dict]:
+ # Assign checkout user to an existing user if checkout email matches a valid
+ # customer account
+ if user is None and checkout_info.checkout.email:
+ existing_user = retrieve_user_by_email(checkout_info.checkout.email)
+ checkout_info.user = (
+ existing_user if existing_user and existing_user.is_active else None
+ )
+
transactions = checkout_info.checkout.payment_transactions.all()
fetch_checkout_data(checkout_info, manager, lines)
diff --git a/saleor/checkout/tests/test_associate_checkout_with_account.py b/saleor/checkout/tests/test_associate_checkout_with_account.py
new file mode 100644
index 0000000000..2b3661412d
--- /dev/null
+++ b/saleor/checkout/tests/test_associate_checkout_with_account.py
@@ -0,0 +1,121 @@
+import pytest
+
+from saleor.channel import MarkAsPaidStrategy
+from saleor.checkout.complete_checkout import complete_checkout
+
+from ...plugins.manager import get_plugins_manager
+from ..fetch import fetch_checkout_info, fetch_checkout_lines
+
+
+ "paid_strategy",
+ [
+ MarkAsPaidStrategy.TRANSACTION_FLOW,
+ MarkAsPaidStrategy.PAYMENT_FLOW,
+ ],
+)
+def test_associate_guest_checkout_with_account_if_exists(
+ paid_strategy, app, address, checkout, customer_user
+):
+ # set the checkout email
+ checkout.email = "[email protected]"
+ checkout.save()
+ user = None
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ checkout_info.billing_address = address
+ checkout_info.channel.order_mark_as_paid_strategy == paid_strategy
+
+ # call the complete_checkout function with the checkout object
+ order, _, _ = complete_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ lines=lines,
+ payment_data={},
+ store_source=False,
+ user=user,
+ app=app,
+ )
+
+ # assert that the order is associated with the correct user
+ assert order.user == customer_user
+
+
+ "paid_strategy",
+ [
+ MarkAsPaidStrategy.TRANSACTION_FLOW,
+ MarkAsPaidStrategy.PAYMENT_FLOW,
+ ],
+)
+def test_associate_guest_checkout_with_account_if_exists_with_guest_user(
+ paid_strategy,
+ app,
+ address,
+ checkout,
+):
+ # set the checkout email
+ checkout.email = "[email protected]"
+ checkout.save()
+ user = None
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ checkout_info.billing_address = address
+ checkout_info.channel.order_mark_as_paid_strategy == paid_strategy
+
+ # call the complete_checkout function with the checkout object
+ order, _, _ = complete_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ lines=lines,
+ payment_data={},
+ store_source=False,
+ user=user,
+ app=app,
+ )
+
+ # assert that the order is associated with the correct user
+ assert order.user is None
+
+
+ "paid_strategy",
+ [
+ MarkAsPaidStrategy.TRANSACTION_FLOW,
+ MarkAsPaidStrategy.PAYMENT_FLOW,
+ ],
+)
+def test_associate_guest_checkout_with_account_if_exists_with_inactive_user(
+ paid_strategy, app, address, checkout, customer_user
+):
+ # set the checkout email
+ checkout.email = "[email protected]"
+ checkout.save()
+ # deactivate the customer user
+ customer_user.is_active = False
+ customer_user.save()
+ user = None
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ checkout_info.billing_address = address
+ checkout_info.channel.order_mark_as_paid_strategy == paid_strategy
+
+ # call the complete_checkout function with the checkout object
+ order, _, _ = complete_checkout(
+ checkout_info=checkout_info,
+ manager=manager,
+ lines=lines,
+ payment_data={},
+ store_source=False,
+ user=user,
+ app=app,
+ )
+
+ # assert that the order is associated with the correct user
+ assert order.user is None