Skip to content

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

Handle zero subtotal case when propagating order discount to lines. (#15950)

Piotr Zabieglik41c71072c629
diff --git a/saleor/order/base_calculations.py b/saleor/order/base_calculations.py
index e5848c04eb..bf97b72d01 100644
--- a/saleor/order/base_calculations.py
+++ b/saleor/order/base_calculations.py
@@ -231,7 +231,9 @@ def propagate_order_discount_on_order_lines_prices(
elif lines_count > 1:
remaining_discount = subtotal_discount
for idx, line in enumerate(lines):
- if idx < lines_count - 1:
+ if not base_subtotal.amount:
+ yield line, zero_money(base_subtotal.currency)
+ elif idx < lines_count - 1:
share = (
line.base_unit_price_amount * line.quantity / base_subtotal.amount
)
diff --git a/saleor/order/tests/test_apply_order_discount.py b/saleor/order/tests/test_apply_order_discount.py
index f22c5dc56f..979d1deb0f 100644
--- a/saleor/order/tests/test_apply_order_discount.py
+++ b/saleor/order/tests/test_apply_order_discount.py
@@ -10,6 +10,7 @@ from ...order.base_calculations import (
apply_order_discounts,
apply_subtotal_discount_to_order_lines,
)
+from ..models import OrderLine
def test_apply_order_discounts_voucher_entire_order(order_with_lines, voucher):
@@ -178,12 +179,16 @@ def test_apply_order_discounts_manual_discount(order_with_lines):
assert order_discount.amount_value == discount_amount
-def test_apply_order_discounts_manual_discount_and_zero_order_total(order):
+def test_apply_order_discounts_zero_discount(order_with_lines):
# given
+ order = order_with_lines
lines = order.lines.all()
- assert not lines
-
currency = order.currency
+ undiscounted_total = order.base_shipping_price_amount + sum(
+ line.undiscounted_total_price_net_amount for line in lines
+ )
+ undiscounted_total = Money(undiscounted_total, currency)
+
order.discounts.create(
type=DiscountType.MANUAL,
value_type=DiscountValueType.FIXED,
@@ -197,6 +202,53 @@ def test_apply_order_discounts_manual_discount_and_zero_order_total(order):
# when
discounted_subtotal, discounted_shipping_price = apply_order_discounts(order, lines)
+ # then
+ assert discounted_subtotal + discounted_shipping_price == undiscounted_total
+
+
+def test_apply_order_discounts_subtotal_zero(order_with_lines):
+ # given
+ order = order_with_lines
+ lines = order.lines.all()
+ for line in lines:
+ line.base_unit_price_amount = Decimal(0)
+ OrderLine.objects.bulk_update(lines, fields=["base_unit_price_amount"])
+
+ currency = order.currency
+ order.discounts.create(
+ type=DiscountType.MANUAL,
+ value_type=DiscountValueType.FIXED,
+ value=10,
+ name="StaffDiscount",
+ translated_name="StaffDiscountPL",
+ currency=currency,
+ )
+
+ # when
+ discounted_subtotal, discounted_shipping_price = apply_order_discounts(order, lines)
+
+ # then
+ assert discounted_subtotal + discounted_shipping_price == zero_money(currency)
+
+
+def test_apply_order_discounts_manual_discount_no_lines(order):
+ # given
+ lines = order.lines.all()
+ assert not lines
+
+ currency = order.currency
+ order.discounts.create(
+ type=DiscountType.MANUAL,
+ value_type=DiscountValueType.FIXED,
+ value=10,
+ name="StaffDiscount",
+ translated_name="StaffDiscountPL",
+ currency=currency,
+ )
+
+ # when
+ discounted_subtotal, discounted_shipping_price = apply_order_discounts(order, lines)
+
# then
assert discounted_subtotal + discounted_shipping_price == zero_money(currency)