Skip to content

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

Allocate stock when draft order completes

Pacu2e7a5372c7f5c
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 49c6fab41e..efe18cca33 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -6,7 +6,7 @@ from ....account.models import Address
from ....core.utils.taxes import ZERO_TAXED_MONEY
from ....order import OrderEvents, OrderStatus, models
from ....order.utils import (
- add_variant_to_order, deallocate_stock, recalculate_order)
+ add_variant_to_order, allocate_stock, recalculate_order)
from ...account.types import AddressInput
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ...core.types.common import Decimal, Error
@@ -116,7 +116,8 @@ class DraftOrderCreate(ModelMutation):
if variants and quantities:
for variant, quantity in zip(variants, quantities):
add_variant_to_order(
- instance, variant, quantity, allow_overselling=True)
+ instance, variant, quantity, allow_overselling=True,
+ track_inventory=False)
recalculate_order(instance)
@@ -202,6 +203,8 @@ class DraftOrderComplete(BaseMutation):
if order.shipping_address:
order.shipping_address.delete()
order.save()
+ for line in order:
+ allocate_stock(line.variant, line.quantity)
order.events.create(
type=OrderEvents.PLACED_FROM_DRAFT.value,
user=info.context.user)
@@ -282,9 +285,6 @@ class DraftOrderLineDelete(BaseMutation):
cls.add_error(
errors, 'id', 'Only draft orders can be edited.')
if not errors:
- variant = line.variant
- if variant.track_inventory:
- deallocate_stock(variant, line.quantity)
db_id = line.id
line.delete()
line.id = db_id
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 6a59bded10..d9d3b660b6 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -150,7 +150,7 @@ def attach_order_to_user(order, user):
def add_variant_to_order(
order, variant, quantity, discounts=None, taxes=None,
- allow_overselling=False):
+ allow_overselling=False, track_inventory=True):
"""Add total_quantity of variant to order.
Returns an order line the variant was added to.
@@ -178,7 +178,7 @@ def add_variant_to_order(
variant=variant,
unit_price=variant.get_price(discounts, taxes),
tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))
- if variant.track_inventory:
+ if variant.track_inventory and track_inventory:
allocate_stock(variant, quantity)
return line