Skip to content

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

Refactor checkout get_total_weight method

IKarbowiakee3c47b6efbf
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 80f5315e26..d6295bbd11 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -116,19 +116,11 @@ class Checkout(ModelWithMetadata):
return zero_money(currency=self.currency)
return Money(balance, self.currency)
- def get_total_weight(
- self, lines: Optional[Iterable["CheckoutLineInfo"]] = None
- ) -> "Weight":
- # Cannot use `sum` as it parses an empty Weight to an int
+ def get_total_weight(self, lines: Iterable["CheckoutLineInfo"]) -> "Weight":
weights = zero_weight()
- # TODO: we should use new data structure for lines in order like in checkout
- if lines is None:
- for line in self:
- weights += line.variant.get_weight() * line.quantity
- else:
- for checkout_line_info in lines:
- line = checkout_line_info.line
- weights += line.variant.get_weight() * line.quantity
+ for checkout_line_info in lines:
+ line = checkout_line_info.line
+ weights += line.variant.get_weight() * line.quantity
return weights
def get_line(self, variant: "ProductVariant") -> Optional["CheckoutLine"]:
diff --git a/saleor/checkout/tests/test_cart.py b/saleor/checkout/tests/test_cart.py
index 7d5b7f6b73..fbb528e00f 100644
--- a/saleor/checkout/tests/test_cart.py
+++ b/saleor/checkout/tests/test_cart.py
@@ -271,4 +271,5 @@ def test_get_total_weight(checkout_with_item):
variant.save()
line.quantity = 6
line.save()
- assert checkout_with_item.get_total_weight() == Weight(kg=60)
+ lines = fetch_checkout_lines(checkout_with_item)
+ assert checkout_with_item.get_total_weight(lines) == Weight(kg=60)