Skip to content

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

Fix attribute value assotiation for multiple attributes (#15663)

Iga Karbowiak652360d90b44
diff --git a/saleor/attribute/tests/test_utils.py b/saleor/attribute/tests/test_utils.py
index a288bf23c7..26edbe00a3 100644
--- a/saleor/attribute/tests/test_utils.py
+++ b/saleor/attribute/tests/test_utils.py
@@ -175,3 +175,40 @@ def test_associate_attribute_to_product_copies_data_over_to_new_field(
(values[0].pk, product.id),
(values[1].pk, product.id),
]
+
+
+def test_associate_attribute_to_instance_duplicated_values(
+ product, attribute_value_generator, multiselect_attribute, color_attribute
+):
+ # Ensure values are properly assigned even if the new value name is the same
+ # as value of different attribute.
+ product.product_type.product_attributes.add(multiselect_attribute, color_attribute)
+ color_attribute_value = color_attribute.values.first()
+
+ # create multiselect value with the same name as color value
+ multiselect_value = attribute_value_generator(
+ attribute=multiselect_attribute,
+ slug=color_attribute_value.slug,
+ name=color_attribute_value.name,
+ )
+ new_color_value = attribute_value_generator(
+ attribute=color_attribute,
+ slug="new-color-value",
+ name="New color value",
+ )
+
+ # Assign new values
+ associate_attribute_values_to_instance(
+ product,
+ {
+ color_attribute.id: [new_color_value],
+ multiselect_attribute.id: [multiselect_value],
+ },
+ )
+
+ # Ensure the new assignment was created
+ assert product.attributevalues.count() == 2
+ assert set(product.attributevalues.values_list("value_id", "product_id")) == {
+ (new_color_value.pk, product.id),
+ (multiselect_value.pk, product.id),
+ }
diff --git a/saleor/attribute/utils.py b/saleor/attribute/utils.py
index a5fa50b8ac..c3ee658524 100644
--- a/saleor/attribute/utils.py
+++ b/saleor/attribute/utils.py
@@ -1,7 +1,8 @@
from collections import defaultdict
+from functools import reduce
from typing import Union
-from django.db.models import Exists, OuterRef
+from django.db.models import Exists, OuterRef, Q
from ..page.models import Page
from ..product.models import Product, ProductVariant
@@ -51,14 +52,22 @@ def associate_attribute_values_to_instance(
def validate_attribute_owns_values(attr_val_map: dict[int, list]) -> None:
+ if not attr_val_map:
+ return
values_map = defaultdict(set)
slug_value_to_value_map = {}
- values = AttributeValue.objects.filter(
- attribute_id__in=attr_val_map.keys(),
- # slug is used as newly created values have no id yet because of
- # using `ignore_conflicts=True` flag in `bulk_create
- slug__in=[v.slug for values in attr_val_map.values() for v in values],
+
+ # we need to fetch the proper values which attribute ids and value slug matches
+ lookup = reduce(
+ lambda acc, val_map_item: acc
+ | Q(
+ attribute_id=val_map_item[0], slug__in=[val.slug for val in val_map_item[1]]
+ ),
+ attr_val_map.items(),
+ Q(),
)
+ values = AttributeValue.objects.filter(lookup)
+
for value in values:
values_map[value.attribute_id].add(value.slug)
slug_value_to_value_map[value.slug] = value
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
index b08626551f..70c5f60b00 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_bulk_update.py
@@ -628,6 +628,72 @@ def test_product_variant_bulk_update_when_variant_not_exists(
assert error["code"] == ProductVariantBulkErrorCode.INVALID.name
+@patch(
+ "saleor.graphql.product.bulk_mutations."
+ "product_variant_bulk_update.get_webhooks_for_event"
+)
+def test_product_variant_bulk_update_attributes(
+ mocked_get_webhooks_for_event,
+ staff_api_client,
+ variant_with_many_stocks,
+ permission_manage_products,
+ any_webhook,
+ settings,
+ multiselect_attribute,
+ color_attribute,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ # given
+ variant = variant_with_many_stocks
+ product_id = graphene.Node.to_global_id("Product", variant.product_id)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ product = variant.product
+ product.product_type.variant_attributes.add(multiselect_attribute, color_attribute)
+ color_attribute_value = color_attribute.values.first()
+ color_attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
+ multiselect_attribute_id = graphene.Node.to_global_id(
+ "Attribute", multiselect_attribute.pk
+ )
+
+ # ensure that providing as a new value for an attribute, name of an existing value
+ # from another attribute will not raise an Error
+ variants = [
+ {
+ "id": variant_id,
+ "attributes": [
+ {"id": color_attribute_id, "dropdown": {"value": "new-value"}},
+ {
+ "id": multiselect_attribute_id,
+ "multiselect": [
+ {"value": color_attribute_value.name},
+ {"value": "test-value-2"},
+ ],
+ },
+ ],
+ }
+ ]
+
+ variables = {"productId": product_id, "variants": variants}
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_VARIANT_BULK_UPDATE_MUTATION, variables
+ )
+ content = get_graphql_content(response)
+ flush_post_commit_hooks()
+ data = content["data"]["productVariantBulkUpdate"]
+
+ # then
+ assert not data["results"][0]["errors"]
+ assert data["count"] == 1
+ for rule in get_active_catalogue_promotion_rules():
+ assert rule.variants_dirty
+
+
@override_settings(ENABLE_LIMITING_WEBHOOKS_FOR_IDENTICAL_PAYLOADS=True)
@mock.patch(
"saleor.graphql.product.bulk_mutations.product_variant_bulk_update.ProductVariantBulkUpdate.call_event"
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 6b6cd0a963..1c24734f9b 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1824,6 +1824,7 @@ def attribute_generator():
slug="attr",
name="Attr",
type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.DROPDOWN,
filterable_in_storefront=True,
filterable_in_dashboard=True,
available_in_grid=True,
@@ -1833,6 +1834,7 @@ def attribute_generator():
slug=slug,
name=name,
type=type,
+ input_type=input_type,
filterable_in_storefront=filterable_in_storefront,
filterable_in_dashboard=filterable_in_dashboard,
available_in_grid=available_in_grid,
@@ -1973,6 +1975,28 @@ def attribute_without_values():
)
+def multiselect_attribute(db, attribute_generator, attribute_values_generator):
+ attribute = attribute_generator(
+ slug="multi",
+ name="Multi",
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.MULTISELECT,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+ slugs = ["choice-1", "choice-1"]
+ names = ["Choice 1", "Choice 2"]
+ attribute_values_generator(
+ attribute=attribute,
+ names=names,
+ slugs=slugs,
+ )
+
+ return attribute
+
+
@pytest.fixture
def product_type_with_product_attributes(attribute_without_values):
product_type = ProductType.objects.create(