Skip to content

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

Improve adding variant to an order

Anna Siwik1085c7504185
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index ef75e38440..81100267c1 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -350,7 +350,8 @@ class Checkout:
for line in self.cart.lines.all():
add_variant_to_order(
- order, line.variant, line.quantity, self.cart.discounts)
+ order, line.variant, line.quantity, self.cart.discounts,
+ add_to_existing=False)
if voucher is not None:
increase_voucher_usage(voucher)
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 99eabd13a0..3c06e0e816 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -62,7 +62,8 @@ def attach_order_to_user(order, user):
order.save(update_fields=['user'])
-def add_variant_to_order(order, variant, total_quantity, discounts=None):
+def add_variant_to_order(
+ order, variant, total_quantity, discounts=None, add_to_existing=True):
"""Add total_quantity of variant to order.
Raises InsufficientStock exception if quantity could not be fulfilled.
@@ -74,7 +75,7 @@ def add_variant_to_order(order, variant, total_quantity, discounts=None):
as long as total_quantity of variant will be added.
"""
quantity_left = add_variant_to_existing_lines(
- order, variant, total_quantity)
+ order, variant, total_quantity) if add_to_existing else total_quantity
price = variant.get_price_per_item(discounts)
while quantity_left > 0:
stock = variant.select_stockrecord()