Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor checkout get_total_weight method
IKarbowiakee3c47b6efbf
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 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.quantityreturn weightsdef get_line(self, variant: "ProductVariant") -> Optional["CheckoutLine"]:diff --git a/saleor/checkout/tests/test_cart.py b/saleor/checkout/tests/test_cart.pyindex 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 = 6line.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)