Skip to content

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

Set variant names in product type update mutation

Adam Janik82fa9347868b
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index a0b1f5a91f..75436a16c6 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1,9 +1,8 @@
import graphene
from django.template.defaultfilters import slugify
from graphene.types import InputObjectType
-from graphql_jwt.decorators import permission_required
-
from graphene_file_upload import Upload
+from graphql_jwt.decorators import permission_required
from ....product import models
from ....product.utils.attributes import get_name_from_attributes
@@ -15,6 +14,23 @@ from ...utils import get_attributes_dict_from_list, get_node, get_nodes
from ..types import Collection, Product, ProductImage, ProductVariant
+def update_variants_names(instance, saved_attributes):
+ initial_attributes = set(instance.variant_attributes.all())
+ attributes_changed = initial_attributes.intersection(saved_attributes)
+ if not attributes_changed:
+ return
+ variants_to_be_updated = models.ProductVariant.objects.filter(
+ product__in=instance.products.all(),
+ product__product_type__variant_attributes__in=attributes_changed)
+ variants_to_be_updated = variants_to_be_updated.prefetch_related(
+ 'product__product_type__variant_attributes__values').all()
+ attributes = instance.variant_attributes.all()
+ for variant in variants_to_be_updated:
+ variant.name = get_name_from_attributes(
+ variant, attributes=attributes)
+ variant.save()
+
+
class CategoryInput(graphene.InputObjectType):
description = graphene.String(description='Category description')
name = graphene.String(description='Category name')
@@ -418,6 +434,14 @@ class ProductTypeUpdate(ProductTypeCreate):
description = 'Updates an existing product type.'
model = models.ProductType
+ @classmethod
+ def save(cls, info, instance, cleaned_input):
+ variant_attr = cleaned_input.get('variant_attributes')
+ if variant_attr:
+ variant_attr = set(variant_attr)
+ update_variants_names(instance, variant_attr)
+ super().save(info, instance, cleaned_input)
+
class ProductTypeDelete(ModelDeleteMutation):
class Arguments:
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index b2d41e2b6a..13d707824f 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1118,7 +1118,6 @@ def test_remove_products_to_collection(
'products']['totalCount'] == no_products_before - len(product_ids)
-
def test_assign_variant_image(admin_api_client, user_api_client, product_with_image):
query = """
mutation assignVariantImageMutation($variantId: ID!, $imageId: ID!) {
@@ -1208,3 +1207,49 @@ def test_unassign_variant_image(admin_api_client, user_api_client, product_with_
response = user_api_client.post(
reverse('api'), {'query': query, 'variables': variables})
assert_no_permission(response)
+
+
+def test_product_type_update_changes_variant_name(
+ admin_api_client, product_type, product):
+ query = """
+ mutation updateProductType(
+ $id: ID!,
+ $hasVariants: Boolean!,
+ $isShippingRequired: Boolean!,
+ $variantAttributes: [ID],
+ ) {
+ productTypeUpdate(
+ id: $id,
+ input: {
+ hasVariants: $hasVariants,
+ isShippingRequired: $isShippingRequired,
+ variantAttributes: $variantAttributes}) {
+ productType {
+ id
+ }
+ }
+ }
+ """
+ has_variants = True
+ require_shipping = False
+ product_type_id = graphene.Node.to_global_id(
+ 'ProductType', product_type.id)
+
+ variant_attributes = product_type.variant_attributes.all()
+ variant_attributes_ids = [
+ graphene.Node.to_global_id('ProductAttribute', att.id) for att in
+ variant_attributes]
+ variables = json.dumps({
+ 'id': product_type_id,
+ 'hasVariants': has_variants,
+ 'isShippingRequired': require_shipping,
+ 'variantAttributes': variant_attributes_ids})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ product.refresh_from_db()
+ variant = product.variants.first()
+ attribute = product.product_type.variant_attributes.first()
+ value = attribute.values.first().name
+ assert variant.name == value