Skip to content

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

Remove corresponding draft order lines when variant is removing

IKarbowiak03c0339c3f81
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index e813f7597f..f5e015d3f6 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -12,6 +12,7 @@ from graphql_relay import from_global_id
from ....core.exceptions import PermissionDenied
from ....core.permissions import ProductPermissions
+from ....order import OrderStatus, models as order_models
from ....product import models
from ....product.error_codes import ProductErrorCode
from ....product.tasks import (
@@ -1372,6 +1373,25 @@ class ProductVariantDelete(ModelDeleteMutation):
update_product_minimal_variant_price_task.delay(instance.product_id)
return super().success_response(instance)
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ node_id = data.get("id")
+ instance = cls.get_node_or_error(info, node_id, only_type=ProductVariant)
+
+ # get draft order lines for variant
+ line_pks = list(
+ order_models.OrderLine.objects.filter(
+ variant=instance, order__status=OrderStatus.DRAFT
+ ).values_list("pk", flat=True)
+ )
+
+ response = super().perform_mutation(_root, info, **data)
+
+ # delete order lines for deleted variant
+ order_models.OrderLine.objects.filter(pk__in=line_pks).delete()
+
+ return response
+
class ProductVariantUpdateMeta(UpdateMetaBaseMutation):
class Meta:
diff --git a/saleor/graphql/product/tests/test_variant.py b/saleor/graphql/product/tests/test_variant.py
index 95e3b62fc2..a8b088e69a 100644
--- a/saleor/graphql/product/tests/test_variant.py
+++ b/saleor/graphql/product/tests/test_variant.py
@@ -4,8 +4,11 @@ from uuid import uuid4
import graphene
import pytest
from measurement.measures import Weight
+from prices import Money, TaxedMoney
from ....core.weight import WeightUnits
+from ....order import OrderStatus
+from ....order.models import OrderLine
from ....product.error_codes import ProductErrorCode
from ....product.models import Product, ProductVariant
from ....product.utils.attributes import associate_attribute_values_to_instance
@@ -999,17 +1002,20 @@ def test_update_product_variant_with_price_does_not_raise_price_validation_error
)
-def test_delete_variant(staff_api_client, product, permission_manage_products):
- query = """
- mutation variantDelete($id: ID!) {
- productVariantDelete(id: $id) {
- productVariant {
- sku
- id
- }
- }
+DELETE_VARIANT_MUTATION = """
+ mutation variantDelete($id: ID!) {
+ productVariantDelete(id: $id) {
+ productVariant {
+ sku
+ id
}
- """
+ }
+ }
+"""
+
+
+def test_delete_variant(staff_api_client, product, permission_manage_products):
+ query = DELETE_VARIANT_MUTATION
variant = product.variants.first()
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
variables = {"id": variant_id}
@@ -1023,6 +1029,47 @@ def test_delete_variant(staff_api_client, product, permission_manage_products):
variant.refresh_from_db()
+def test_delete_variant_in_draft_order(
+ staff_api_client, order_line, permission_manage_products, order_list
+):
+ query = DELETE_VARIANT_MUTATION
+
+ draft_order = order_line.order
+ draft_order.status = OrderStatus.DRAFT
+ draft_order.save(update_fields=["status"])
+
+ variant = order_line.variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ variables = {"id": variant_id}
+
+ net = variant.get_price()
+ gross = Money(amount=net.amount, currency=net.currency)
+ order_not_draft = order_list[-1]
+ order_line_not_in_draft = OrderLine.objects.create(
+ variant=variant,
+ order=order_not_draft,
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ unit_price=TaxedMoney(net=net, gross=gross),
+ quantity=3,
+ )
+ order_line_not_in_draft_pk = order_line_not_in_draft.pk
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantDelete"]
+ assert data["productVariant"]["sku"] == variant.sku
+ with pytest.raises(order_line._meta.model.DoesNotExist):
+ order_line.refresh_from_db()
+
+ assert OrderLine.objects.filter(pk=order_line_not_in_draft_pk).exists()
+
+
def _fetch_all_variants(client, permissions=None):
query = """
query fetchAllVariants {