Skip to content

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

Prevent name overwritting of Product Variants when Updating Product Types (#15670)

Teddy Ondiekid3fa53c48cd3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5031460a04..0817889b10 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Don't raise InsufficientStock for track_inventory=False variants #15475 by @carlosa54
- DB performance improvements in attribute dataloaders - #15474 by @AjmalPonneth
- Calculate order promotions in draft orders - #15459 by @zedzior
+- Prevent name overwriting of Product Variants when Updating Product Types - #15670 by @teddyondieki
# 3.19.0
diff --git a/saleor/product/tasks.py b/saleor/product/tasks.py
index 1d0e3daf3d..5de5ee0a63 100644
--- a/saleor/product/tasks.py
+++ b/saleor/product/tasks.py
@@ -58,8 +58,8 @@ def _variants_in_batches(variants_qs):
def _update_variants_names(instance: ProductType, saved_attributes: Iterable):
"""Product variant names are created from names of assigned attributes.
- After change in attribute value name, for all product variants using this
- attributes we need to update the names.
+ After change in attribute value name, we update the names for all product variants
+ that lack names and use these attributes.
"""
initial_attributes = set(instance.variant_attributes.all())
attributes_changed = initial_attributes.intersection(saved_attributes)
@@ -67,6 +67,7 @@ def _update_variants_names(instance: ProductType, saved_attributes: Iterable):
return
variants = ProductVariant.objects.filter(
+ name="",
product__in=instance.products.all(),
product__product_type__variant_attributes__in=attributes_changed,
)
diff --git a/saleor/product/tests/test_tasks.py b/saleor/product/tests/test_tasks.py
index 6238e8a078..9c1346d9b6 100644
--- a/saleor/product/tests/test_tasks.py
+++ b/saleor/product/tests/test_tasks.py
@@ -5,6 +5,7 @@ from unittest.mock import patch
import pytest
from django.utils import timezone
+from faker import Faker
from ...discount import PromotionType, RewardValueType
from ...discount.models import Promotion, PromotionRule
@@ -230,17 +231,23 @@ def test_recalculate_discounted_price_for_products_task_re_trigger_task(
assert recalculate_discounted_price_for_products_task_mock.called
-@patch("saleor.product.tasks._update_variants_names")
-def test_update_variants_names(
- update_variants_names_mock, product_type, size_attribute
-):
+def test_update_variants_names(product_variant_list, size_attribute):
+ # given
+ variant_without_name = product_variant_list[0]
+ variant_with_name = product_variant_list[1]
+ random_name = Faker().word()
+ variant_with_name.name = random_name
+ variant_with_name.save()
+ product = variant_without_name.product
+
# when
- update_variants_names(product_type.id, [size_attribute.id])
+ update_variants_names(product.product_type_id, [size_attribute.id])
# then
- args, _ = update_variants_names_mock.call_args
- assert args[0] == product_type
- assert {arg.pk for arg in args[1]} == {size_attribute.pk}
+ variant_without_name.refresh_from_db()
+ variant_with_name.refresh_from_db()
+ assert variant_without_name.name == variant_without_name.sku
+ assert variant_with_name.name == random_name
def test_update_variants_names_product_type_does_not_exist(caplog):