Skip to content

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

Split create_order to multiple functions

Anna Siwikc04f6cd0426a
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index f8d6382f09..5987199cbf 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -737,73 +737,64 @@ def check_shipping_method(cart):
return True
-def create_order(cart, tracking_code, discounts, taxes):
- """Create an order from the cart.
+def _process_voucher_data_for_order(cart):
+ """Fetch, process and return voucher/discount data from cart."""
+ vouchers = Voucher.objects.active(date=date.today()).select_for_update()
+ voucher = get_voucher_for_cart(cart, vouchers)
- Each order will get a private copy of both the billing and the shipping
- address (if shipping).
+ if cart.voucher_code and not voucher:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'Voucher expired in meantime. Order placement aborted.')
+ raise NotApplicable(msg)
- If any of the addresses is new and the user is logged in the address
- will also get saved to that user's address book.
+ if not voucher:
+ return {}
- Current user's language is saved in the order so we can later determine
- which language to use when sending email.
- """
- # FIXME: save locale along with the language
- from ..order.utils import add_variant_to_order
+ increase_voucher_usage(voucher)
+ return {
+ 'voucher': voucher,
+ 'discount_amount': cart.discount_amount,
+ 'discount_name': cart.discount_name}
- voucher = get_voucher_for_cart(
- cart, vouchers=Voucher.objects.active(
- date=date.today()).select_for_update())
- if cart.voucher_code and not voucher:
- # Voucher expired in meantime, abort order placement
- return None
+def _process_shipping_data_for_order(cart, taxes):
+ """Fetch, process and return shipping data from cart."""
+ if not cart.is_shipping_required():
+ return {}
- billing_address = cart.billing_address
+ shipping_address = cart.shipping_address
- if cart.is_shipping_required():
- shipping_address = cart.shipping_address
- shipping_method = cart.shipping_method
- shipping_method_name = smart_text(shipping_method)
- else:
- shipping_address = None
- shipping_method = None
- shipping_method_name = None
+ if cart.user:
+ store_user_address(cart.user, shipping_address, AddressType.SHIPPING)
+ if cart.user.addresses.filter(pk=shipping_address.pk).exists():
+ shipping_address = shipping_address.get_copy()
+
+ return {
+ 'shipping_address': shipping_address,
+ 'shipping_method': cart.shipping_method,
+ 'shipping_method_name': smart_text(cart.shipping_method),
+ 'shipping_price': cart.get_shipping_price(taxes)}
+
+
+def _process_user_data_for_order(cart):
+ """Fetch, process and return shipping data from cart."""
+ billing_address = cart.billing_address
if cart.user:
store_user_address(cart.user, billing_address, AddressType.BILLING)
-
if cart.user.addresses.filter(pk=billing_address.pk).exists():
billing_address = billing_address.get_copy()
- if cart.is_shipping_required():
- store_user_address(
- cart.user, shipping_address, AddressType.SHIPPING)
-
- if cart.user.addresses.filter(pk=shipping_address.pk).exists():
- shipping_address = shipping_address.get_copy()
-
- order_data = {
- 'language_code': get_language(),
+ return {
'user': cart.user,
'user_email': cart.user.email if cart.user else cart.email,
- 'billing_address': billing_address,
- 'shipping_address': shipping_address,
- 'tracking_client_id': tracking_code,
- 'shipping_method': shipping_method,
- 'shipping_method_name': shipping_method_name,
- 'shipping_price': cart.get_shipping_price(taxes),
- 'total': cart.get_total(discounts, taxes)}
+ 'billing_address': billing_address}
- if voucher:
- order_data['voucher'] = voucher
- order_data['discount_amount'] = cart.discount_amount
- order_data['discount_name'] = cart.discount_name
- increase_voucher_usage(voucher)
- order = Order.objects.create(**order_data)
+def _fill_order_with_cart_data(order, cart, discounts, taxes):
+ """Fill an order with data (variants, note) from cart."""
+ from ..order.utils import add_variant_to_order
for line in cart:
add_variant_to_order(
@@ -812,4 +803,34 @@ def create_order(cart, tracking_code, discounts, taxes):
if cart.note:
order.notes.create(user=order.user, content=cart.note)
+
+def create_order(cart, tracking_code, discounts, taxes):
+ """Create an order from the cart.
+
+ Each order will get a private copy of both the billing and the shipping
+ address (if shipping).
+
+ If any of the addresses is new and the user is logged in the address
+ will also get saved to that user's address book.
+
+ Current user's language is saved in the order so we can later determine
+ which language to use when sending email.
+ """
+ # FIXME: save locale along with the language
+ try:
+ order_data = _process_voucher_data_for_order(cart)
+ except NotApplicable:
+ return None
+
+ order_data.update(_process_shipping_data_for_order(cart, taxes))
+ order_data.update(_process_user_data_for_order(cart))
+ order_data.update({
+ 'language_code': get_language(),
+ 'tracking_client_id': tracking_code,
+ 'total': cart.get_total(discounts, taxes)})
+
+ order = Order.objects.create(**order_data)
+
+ _fill_order_with_cart_data(order, cart, discounts, taxes)
return order