Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix bug with handling orders containing deleted variants in 3.1
jakubkuc59d1fb346a62
diff --git a/saleor/order/notifications.py b/saleor/order/notifications.pyindex 2956c8db75..d9749dc865 100644--- a/saleor/order/notifications.py+++ b/saleor/order/notifications.py@@ -91,13 +91,19 @@ def get_order_line_payload(line: "OrderLine"):if line.is_digital:content = DigitalContentUrl.objects.filter(line=line).first()digital_url = content.get_absolute_url() if content else None # type: ignore+ variant_dependent_fields = {}+ if line.variant:+ variant_dependent_fields = {+ "product": get_product_payload(line.variant.product),+ "variant": get_product_variant_payload(line.variant),+ }return {"id": line.id,- "product": get_product_payload(line.variant.product), # type: ignore+ "product": variant_dependent_fields.get("product"), # type: ignore"product_name": line.product_name,"translated_product_name": line.translated_product_name or line.product_name,"variant_name": line.variant_name,- "variant": get_product_variant_payload(line.variant), # type: ignore+ "variant": variant_dependent_fields.get("variant"), # type: ignore"translated_variant_name": line.translated_variant_name or line.variant_name,"product_sku": line.product_sku,"quantity": line.quantity,diff --git a/saleor/order/tests/test_notifications.py b/saleor/order/tests/test_notifications.pyindex 2316edfd25..a39647d799 100644--- a/saleor/order/tests/test_notifications.py+++ b/saleor/order/tests/test_notifications.py@@ -160,6 +160,14 @@ def test_get_order_line_payload(order_line):}+def test_get_order_line_payload_deleted_variant(order_line):+ order_line.variant = None+ payload = get_order_line_payload(order_line)++ assert payload["variant"] is None+ assert payload["product"] is None++def test_get_address_payload(address):payload = get_address_payload(address)assert payload == {diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.pyindex b765e426cb..a84fa8258a 100644--- a/saleor/webhook/payloads.py+++ b/saleor/webhook/payloads.py@@ -473,10 +473,16 @@ def generate_fulfillment_lines_payload(fulfillment: Fulfillment):"product_name": lambda fl: fl.order_line.product_name,"variant_name": lambda fl: fl.order_line.variant_name,"product_sku": lambda fl: fl.order_line.product_sku,- "weight": (lambda fl: fl.order_line.variant.get_weight().g),+ "weight": (+ lambda fl: fl.order_line.variant.get_weight().g+ if fl.order_line.variant+ else None+ ),"weight_unit": "gram","product_type": (lambda fl: fl.order_line.variant.product.product_type.name+ if fl.order_line.variant+ else None),"unit_price_net": lambda fl: fl.order_line.unit_price_net_amount,"unit_price_gross": lambda fl: fl.order_line.unit_price_gross_amount,diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.pyindex 3f78aa21f7..f38b368fda 100644--- a/saleor/webhook/tests/test_webhook_payloads.py+++ b/saleor/webhook/tests/test_webhook_payloads.py@@ -156,6 +156,27 @@ def test_generate_fulfillment_lines_payload(order_with_lines):}+def test_generate_fulfillment_lines_payload_deleted_variant(order_with_lines):+ # given+ fulfillment = order_with_lines.fulfillments.create(tracking_number="123")+ line = order_with_lines.lines.first()+ stock = line.allocations.get().stock+ warehouse_pk = stock.warehouse.pk+ fulfillment.lines.create(order_line=line, quantity=line.quantity, stock=stock)+ fulfill_order_lines(+ [OrderLineData(line=line, quantity=line.quantity, warehouse_pk=warehouse_pk)],+ get_plugins_manager(),+ )++ # when+ line.variant.delete()+ payload = json.loads(generate_fulfillment_lines_payload(fulfillment))[0]++ # then+ assert payload["product_type"] is None+ assert payload["weight"] is None++def test_order_lines_have_all_required_fields(order, order_line_with_one_allocation):order.lines.add(order_line_with_one_allocation)line = order_line_with_one_allocation