Skip to content

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

Decorate recalculate_order to recalculate voucher

Anna Siwikfa012f4f11fa
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 5ddc78d7ef..8f5a8d9c5b 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -181,8 +181,8 @@ class OrderShippingForm(forms.ModelForm):
method = self.instance.shipping_method
self.instance.shipping_method_name = method.shipping_method.name
self.instance.shipping_price = method.get_total_price()
- recalculate_order(
- self.instance, get_voucher_discount_for_order(self.instance))
+ recalculate_order(self.instance)
+ recalculate_order(self.instance)
return super().save(commit)
@@ -197,8 +197,7 @@ class OrderRemoveShippingForm(forms.ModelForm):
self.instance.shipping_method = None
self.instance.shipping_method_name = None
self.instance.shipping_price = ZERO_TAXED_MONEY
- recalculate_order(
- self.instance, get_voucher_discount_for_order(self.instance))
+ recalculate_order(self.instance)
return super().save(commit)
@@ -214,7 +213,7 @@ class OrderEditDiscountForm(forms.ModelForm):
'Discount amount')}
def save(self, commit=True):
- recalculate_order(self.instance)
+ recalculate_order(self.instance, update_voucher_discount=False)
return super().save(commit)
@@ -243,8 +242,7 @@ class OrderEditVoucherForm(forms.ModelForm):
decrease_voucher_usage(self.old_voucher)
increase_voucher_usage(voucher)
self.instance.discount_name = voucher.name or ''
- recalculate_order(
- self.instance, get_voucher_discount_for_order(self.instance))
+ recalculate_order(self.instance)
return super().save(commit)
@@ -393,7 +391,7 @@ class CancelOrderLineForm(forms.Form):
deallocate_stock(self.line.stock, self.line.quantity)
order = self.line.order
self.line.delete()
- recalculate_order(order, get_voucher_discount_for_order(order))
+ recalculate_order(order)
class ChangeQuantityForm(forms.ModelForm):
@@ -435,8 +433,7 @@ class ChangeQuantityForm(forms.ModelForm):
delta = quantity - self.initial_quantity
allocate_stock(stock, delta)
change_order_line_quantity(self.instance, quantity)
- order = self.instance.order
- recalculate_order(order, get_voucher_discount_for_order(order))
+ recalculate_order(self.instance.order)
return self.instance
@@ -642,8 +639,7 @@ class AddVariantToOrderForm(forms.Form):
quantity = self.cleaned_data.get('quantity')
add_variant_to_order(
self.order, variant, quantity, self.discounts)
- recalculate_order(
- self.order, get_voucher_discount_for_order(self.order))
+ recalculate_order(self.order)
class AddressForm(StorefrontAddressForm):
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 5cb7388cff..bdb17f7664 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -7,6 +7,7 @@ from prices import Money
from ..account.utils import store_user_address
from ..core.exceptions import InsufficientStock
+from ..dashboard.order.utils import get_voucher_discount_for_order
from ..order import FulfillmentStatus, OrderStatus
from ..product.utils import allocate_stock, deallocate_stock, increase_stock
@@ -33,19 +34,35 @@ def check_order_status(func):
return decorator
-def recalculate_order(order, discount_amount=None):
+def update_voucher_discount(func):
+ """Recalculate order discount amount based on order voucher."""
+
+ @wraps(func)
+ def decorator(*args, **kwargs):
+ if kwargs.pop('update_voucher_discount', True):
+ order = args[0]
+ order.discount_amount = (
+ get_voucher_discount_for_order(order) or
+ Money(0, settings.DEFAULT_CURRENCY))
+ return func(*args, **kwargs)
+
+ return decorator
+
+
+@update_voucher_discount
+def recalculate_order(order, **kwargs):
"""Recalculate and assign total price of order.
Total price is a sum of items in order and order shipping price minus
discount amount.
+
+ Voucher discount amount is recalculated by default. To avoid this, pass
+ update_voucher_discount argument set to False.
"""
prices = [line.get_total() for line in order]
total = sum(prices, order.shipping_price)
# discount amount can't be greater than order total
- current_discount_amount = (
- discount_amount or order.discount_amount or
- Money(0, settings.DEFAULT_CURRENCY))
- order.discount_amount = min(current_discount_amount, total.gross)
+ order.discount_amount = min(order.discount_amount, total.gross)
if order.discount_amount:
total -= order.discount_amount
order.total = total