Skip to content

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

Remove variants corresponding draft order lines when product type in removing

IKarbowiak4c7f1bf8c67d
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index ec6a1742da..59bb85105f 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -45,7 +45,14 @@ from ...core.utils.reordering import perform_reordering
from ...core.validators import validate_price_precision
from ...meta.deprecated.mutations import ClearMetaBaseMutation, UpdateMetaBaseMutation
from ...warehouse.types import Warehouse
-from ..types import Category, Collection, Product, ProductImage, ProductVariant
+from ..types import (
+ Category,
+ Collection,
+ Product,
+ ProductImage,
+ ProductType,
+ ProductVariant,
+)
from ..utils import (
create_stocks,
get_used_attribute_values_for_variant,
@@ -1581,6 +1588,27 @@ class ProductTypeDelete(ModelDeleteMutation):
error_type_class = ProductError
error_type_field = "product_errors"
+ @classmethod
+ def perform_mutation(cls, _root, info, **data):
+ node_id = data.get("id")
+ product_type_pk = from_global_id_strict_type(node_id, ProductType, field="pk")
+ variants_pks = models.Product.objects.filter(
+ product_type__pk=product_type_pk
+ ).values_list("variants__pk", flat=True)
+ # get draft order lines for products
+ order_line_pks = list(
+ order_models.OrderLine.objects.filter(
+ variant__pk__in=variants_pks, order__status=OrderStatus.DRAFT
+ ).values_list("pk", flat=True)
+ )
+
+ response = super().perform_mutation(_root, info, **data)
+
+ # delete order lines for deleted variants
+ order_models.OrderLine.objects.filter(pk__in=order_line_pks).delete()
+
+ return response
+
class ProductTypeUpdateMeta(UpdateMetaBaseMutation):
class Meta:
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 405808fa20..7412f085b9 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -3420,18 +3420,70 @@ def test_update_product_type_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
+PRODUCT_TYPE_DELETE_MUTATION = """
+ mutation deleteProductType($id: ID!) {
+ productTypeDelete(id: $id) {
+ productType {
+ name
+ }
+ }
+ }
+"""
+
+
def test_product_type_delete_mutation(
staff_api_client, product_type, permission_manage_products
):
- query = """
- mutation deleteProductType($id: ID!) {
- productTypeDelete(id: $id) {
- productType {
- name
- }
- }
- }
- """
+ query = PRODUCT_TYPE_DELETE_MUTATION
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.id)}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productTypeDelete"]
+ assert data["productType"]["name"] == product_type.name
+ with pytest.raises(product_type._meta.model.DoesNotExist):
+ product_type.refresh_from_db()
+
+
+def test_product_type_delete_mutation_variants_in_draft_order(
+ staff_api_client, permission_manage_products, product, order_list
+):
+ query = PRODUCT_TYPE_DELETE_MUTATION
+ product_type = product.product_type
+
+ variant = product.variants.first()
+
+ order_not_draft = order_list[-1]
+ draft_order = order_list[1]
+ draft_order.status = OrderStatus.DRAFT
+ draft_order.save(update_fields=["status"])
+
+ net = variant.get_price()
+ gross = Money(amount=net.amount, currency=net.currency)
+
+ 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_in_draft = OrderLine.objects.create(
+ variant=variant,
+ order=draft_order,
+ 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,
+ )
+
variables = {"id": graphene.Node.to_global_id("ProductType", product_type.id)}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products]
@@ -3442,6 +3494,11 @@ def test_product_type_delete_mutation(
with pytest.raises(product_type._meta.model.DoesNotExist):
product_type.refresh_from_db()
+ with pytest.raises(order_line_in_draft._meta.model.DoesNotExist):
+ order_line_in_draft.refresh_from_db()
+
+ assert OrderLine.objects.filter(pk=order_line_not_in_draft.pk).exists()
+
def test_product_image_create_mutation(
monkeypatch, staff_api_client, product, permission_manage_products, media_root