Skip to content

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

Fix productVariantStocksUpdate with empty stocks list

fowczreke1ff0eefc3b5
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.py
index e3fbca2774..27f1e4408d 100644
--- a/saleor/graphql/product/bulk_mutations/products.py
+++ b/saleor/graphql/product/bulk_mutations/products.py
@@ -437,14 +437,15 @@ class ProductVariantStocksUpdate(ProductVariantStocksCreate):
variant = cls.get_node_or_error(
info, data["variant_id"], only_type=ProductVariant
)
- warehouse_ids = [stock["warehouse"] for stock in stocks]
- cls.check_for_duplicates(warehouse_ids, errors)
- if errors:
- raise ValidationError(errors)
- warehouses = cls.get_nodes_or_error(
- warehouse_ids, "warehouse", only_type=Warehouse
- )
- cls.update_or_create_variant_stocks(variant, stocks, warehouses)
+ if stocks:
+ warehouse_ids = [stock["warehouse"] for stock in stocks]
+ cls.check_for_duplicates(warehouse_ids, errors)
+ if errors:
+ raise ValidationError(errors)
+ warehouses = cls.get_nodes_or_error(
+ warehouse_ids, "warehouse", only_type=Warehouse
+ )
+ cls.update_or_create_variant_stocks(variant, stocks, warehouses)
return cls(product_variant=variant)
@classmethod
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index fec212ced8..62ffc6d1ac 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -1708,6 +1708,24 @@ def test_product_variant_stocks_update(
assert res in expected_result
+def test_product_variant_stocks_update_with_empty_stock_list(
+ staff_api_client, variant, warehouse, permission_manage_products
+):
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ stocks = []
+ variables = {"variantId": variant_id, "stocks": stocks}
+ response = staff_api_client.post_graphql(
+ VARIANT_STOCKS_UPDATE_MUTATIONS,
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantStocksUpdate"]
+
+ assert not data["bulkStockErrors"]
+ assert len(data["productVariant"]["stocks"]) == len(stocks)
+
+
def test_variant_stocks_update_stock_duplicated_warehouse(
staff_api_client, variant, warehouse, permission_manage_products
):