Skip to content

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

Fix recalculate_order dismissing weight unit (#9527)

Tomasz Szymański9aea317747e3
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index 3ef9ef3c08..7312460f64 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -78,6 +78,13 @@ def test_order_get_subtotal(order_with_lines):
assert order_with_lines.get_subtotal() == target_subtotal
+def test_recalculate_order_keeps_weight_unit(order_with_lines):
+ initial_weight_unit = order_with_lines.weight.unit
+ recalculate_order(order_with_lines)
+ recalculated_weight_unit = order_with_lines.weight.unit
+ assert initial_weight_unit == recalculated_weight_unit
+
+
def test_add_variant_to_order_adds_line_for_new_variant(
order_with_lines, product, product_translation_fr, settings, info, site_settings
):
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 05907a1c07..7b6de1eb4d 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -195,6 +195,7 @@ def recalculate_order_weight(order):
for line in order.lines.all():
if line.variant:
weight += line.variant.get_weight() * line.quantity
+ weight.unit = order.weight.unit
order.weight = weight
order.save(update_fields=["weight", "updated_at"])
diff --git a/saleor/plugins/webhook/shipping.py b/saleor/plugins/webhook/shipping.py
index ea849576dd..1428569b05 100644
--- a/saleor/plugins/webhook/shipping.py
+++ b/saleor/plugins/webhook/shipping.py
@@ -54,18 +54,15 @@ def parse_list_shipping_methods_response(
def _compare_order_payloads(payload: str, cached_payload: str) -> bool:
- """Compare two string of order payloads.
-
- Compare them while ignoring excluded_keys.
- """
- EXCLUDED_KEYS = ["weight", "meta"]
+ """Compare two strings of order payloads ignoring meta."""
+ EXCLUDED_KEY = "meta"
try:
order_payload = json.loads(payload)["order"]
cached_order_payload = json.loads(cached_payload)["order"]
except: # noqa
return False
- return {k: v for k, v in order_payload.items() if k not in EXCLUDED_KEYS} == {
- k: v for k, v in cached_order_payload.items() if k not in EXCLUDED_KEYS
+ return {k: v for k, v in order_payload.items() if k != EXCLUDED_KEY} == {
+ k: v for k, v in cached_order_payload.items() if k != EXCLUDED_KEY
}