Skip to content

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

Order subtotal should be able to trigger price recalculation (#10905)

Filip Owczarekf12fecc21aeb
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 1c7b425d12..6cb51e0de3 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -36,11 +36,7 @@ from ...order.utils import (
)
from ...payment import ChargeStatus
from ...payment.dataloaders import PaymentsByOrderIdLoader
-from ...payment.model_helpers import (
- get_last_payment,
- get_subtotal,
- get_total_authorized,
-)
+from ...payment.model_helpers import get_last_payment, get_total_authorized
from ...product import ProductMediaTypes
from ...product.models import ALL_PRODUCTS_PERMISSIONS
from ...shipping.interface import ShippingMethodData
@@ -1219,8 +1215,10 @@ class Order(ModelObjectType):
@staticmethod
@traced_resolver
def resolve_subtotal(root: models.Order, info):
+ manager = load_plugin_manager(info.context)
+
def _resolve_subtotal(order_lines):
- return get_subtotal(order_lines, root.currency)
+ return calculations.order_subtotal(root, manager, order_lines)
return (
OrderLinesByOrderIdLoader(info.context)
diff --git a/saleor/order/calculations.py b/saleor/order/calculations.py
index fe78804857..ae997b54bb 100644
--- a/saleor/order/calculations.py
+++ b/saleor/order/calculations.py
@@ -1,5 +1,5 @@
from decimal import Decimal
-from typing import TYPE_CHECKING, Iterable, Optional, Tuple
+from typing import TYPE_CHECKING, Iterable, Optional, Tuple, cast
from django.db import transaction
from django.db.models import prefetch_related_objects
@@ -9,6 +9,7 @@ from ..checkout import base_calculations
from ..core.prices import quantize_price
from ..core.taxes import TaxData, TaxError, zero_taxed_money
from ..order import base_calculations as base_order_calculations
+from ..payment.model_helpers import get_subtotal
from ..plugins.manager import PluginsManager
from ..site.models import Site
from . import ORDER_EDITABLE_STATUS
@@ -362,6 +363,26 @@ def order_shipping_tax_rate(
return order.shipping_tax_rate
+def order_subtotal(
+ order: Order,
+ manager: PluginsManager,
+ lines: Iterable[OrderLine],
+ force_update: bool = False,
+):
+ """Return the total price of the order.
+
+ It takes into account all plugins.
+ If the prices are expired, call all order price calculation methods
+ and save them in the model directly.
+ """
+ currency = order.currency
+ order, lines = fetch_order_prices_if_expired(order, manager, lines, force_update)
+ # Lines aren't returned only if
+ # we don't pass them to `fetch_order_prices_if_expired`.
+ lines = cast(Iterable[OrderLine], lines)
+ return quantize_price(get_subtotal(lines, currency), currency)
+
+
def order_total(
order: Order,
manager: PluginsManager,
diff --git a/saleor/order/tests/test_calculations.py b/saleor/order/tests/test_calculations.py
index e23e981c91..aacf992e8f 100644
--- a/saleor/order/tests/test_calculations.py
+++ b/saleor/order/tests/test_calculations.py
@@ -862,6 +862,34 @@ def test_order_total(mocked_fetch_order_prices_if_expired):
assert total == quantize_price(expected_total, order.currency)
+@patch("saleor.order.calculations.fetch_order_prices_if_expired")
+def test_order_subtotal(mocked_fetch_order_prices_if_expired):
+ # given
+ currency = "USD"
+ manager = Mock()
+ expected_line_totals = [
+ TaxedMoney(Money(Decimal("1.00"), currency), Money(Decimal("1.00"), currency)),
+ TaxedMoney(Money(Decimal("2.00"), currency), Money(Decimal("2.00"), currency)),
+ TaxedMoney(Money(Decimal("4.00"), currency), Money(Decimal("4.00"), currency)),
+ ]
+ order = Mock(currency=currency)
+ lines = []
+ for expected_line_total in expected_line_totals:
+ line = Mock(total_price=expected_line_total, currency=currency)
+ lines.append(line)
+ mocked_fetch_order_prices_if_expired.return_value = (order, lines)
+
+ # when
+ subtotal = calculations.order_subtotal(order, manager, lines)
+
+ # then
+ expected_subtotal = quantize_price(
+ TaxedMoney(Money(Decimal("7.00"), currency), Money(Decimal("7.00"), currency)),
+ currency,
+ )
+ assert subtotal == expected_subtotal
+
+
@patch("saleor.order.calculations.fetch_order_prices_if_expired")
def test_order_undiscounted_total(mocked_fetch_order_prices_if_expired):
# given
diff --git a/saleor/payment/model_helpers.py b/saleor/payment/model_helpers.py
index 0013d70963..764921b5ce 100644
--- a/saleor/payment/model_helpers.py
+++ b/saleor/payment/model_helpers.py
@@ -1,5 +1,5 @@
from operator import attrgetter
-from typing import TYPE_CHECKING, List
+from typing import TYPE_CHECKING, Iterable, List
from ..core.taxes import zero_money, zero_taxed_money
from .models import Payment
@@ -24,6 +24,6 @@ def get_total_authorized(payments: List[Payment], fallback_currency: str):
return zero_money(fallback_currency)
-def get_subtotal(order_lines: List["OrderLine"], fallback_currency: str):
+def get_subtotal(order_lines: Iterable["OrderLine"], fallback_currency: str):
subtotal_iterator = (line.total_price for line in order_lines)
return sum(subtotal_iterator, zero_taxed_money(currency=fallback_currency))