Skip to content

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

Remove variants corresponding draft order lines when products in bulk are removing

IKarbowiak3cd1d4eaf91d
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index 6b50872d9d..343068bfb2 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -36,7 +36,7 @@ from ..mutations.products import (
ProductVariantInput,
StockInput,
)
-from ..types import ProductVariant
+from ..types import Product, ProductVariant
from ..utils import create_stocks, get_used_variants_attribute_values
@@ -109,6 +109,24 @@ class ProductBulkDelete(ModelBulkDeleteMutation):
error_type_class = ProductError
error_type_field = "product_errors"
+ @classmethod
+ def perform_mutation(cls, _root, info, ids, **data):
+ nodes_type, pks = resolve_global_ids_to_primary_keys(ids, Product)
+ variants = models.ProductVariant.objects.filter(product__pk__in=pks)
+ # get draft order lines for products
+ order_line_pks = list(
+ order_models.OrderLine.objects.filter(
+ variant__in=variants, order__status=OrderStatus.DRAFT
+ ).values_list("pk", flat=True)
+ )
+
+ response = super().perform_mutation(_root, info, ids, **data)
+
+ # delete order lines for deleted variants
+ order_models.OrderLine.objects.filter(pk__in=order_line_pks).delete()
+
+ return response
+
class ProductVariantBulkCreateInput(ProductVariantInput):
attributes = graphene.List(
diff --git a/saleor/graphql/product/tests/test_bulk_delete.py b/saleor/graphql/product/tests/test_bulk_delete.py
index 0c25f25059..fd79d6ae57 100644
--- a/saleor/graphql/product/tests/test_bulk_delete.py
+++ b/saleor/graphql/product/tests/test_bulk_delete.py
@@ -216,14 +216,85 @@ def test_delete_collections(
).exists()
-def test_delete_products(staff_api_client, product_list, permission_manage_products):
- query = """
- mutation productBulkDelete($ids: [ID]!) {
- productBulkDelete(ids: $ids) {
- count
- }
+DELETE_PRODUCTS_MUTATION = """
+mutation productBulkDelete($ids: [ID]!) {
+ productBulkDelete(ids: $ids) {
+ count
}
- """
+}
+"""
+
+
+def test_delete_products(
+ staff_api_client, product_list, permission_manage_products, order_list
+):
+ # given
+ query = DELETE_PRODUCTS_MUTATION
+
+ not_draft_order = order_list[0]
+ draft_order = order_list[1]
+ draft_order.status = OrderStatus.DRAFT
+ draft_order.save(update_fields=["status"])
+
+ draft_order_lines_pks = []
+ not_draft_order_lines_pks = []
+ for variant in [product_list[0].variants.first(), product_list[1].variants.first()]:
+ net = variant.get_price()
+ gross = Money(amount=net.amount, currency=net.currency)
+
+ order_line = 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,
+ )
+ draft_order_lines_pks.append(order_line.pk)
+
+ order_line_not_draft = OrderLine.objects.create(
+ variant=variant,
+ order=not_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,
+ )
+ not_draft_order_lines_pks.append(order_line_not_draft.pk)
+
+ variables = {
+ "ids": [
+ graphene.Node.to_global_id("Product", product.id)
+ for product in product_list
+ ]
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+
+ assert content["data"]["productBulkDelete"]["count"] == 3
+ assert not Product.objects.filter(
+ id__in=[product.id for product in product_list]
+ ).exists()
+
+ assert not OrderLine.objects.filter(pk__in=draft_order_lines_pks).exists()
+
+ assert OrderLine.objects.filter(pk__in=not_draft_order_lines_pks).exists()
+
+
+def test_delete_products_variants_in_draft_order(
+ staff_api_client, product_list, permission_manage_products
+):
+ query = DELETE_PRODUCTS_MUTATION
variables = {
"ids": [
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index c0530b9c81..405808fa20 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -2909,8 +2909,6 @@ def test_delete_product_variant_in_draft_order(
product.refresh_from_db()
assert node_id == data["product"]["id"]
- with pytest.raises(order_line._meta.model.DoesNotExist):
- order_line.refresh_from_db()
assert not OrderLine.objects.filter(pk__in=draft_order_lines_pks).exists()
assert OrderLine.objects.filter(pk__in=not_draft_order_lines_pks).exists()