Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change the way variant attributes are being displayed
Adam Janikf6173a98ebfc
diff --git a/saleor/dashboard/graphql/product/forms.py b/saleor/dashboard/graphql/product/forms.pyindex 1b00b9bd1d..de802e414a 100644--- a/saleor/dashboard/graphql/product/forms.py+++ b/saleor/dashboard/graphql/product/forms.py@@ -1,9 +1,10 @@from django import formsfrom ....product.models import Product+from ....graphql.product.fields import AttributeFieldclass ProductForm(forms.ModelForm):"""Base form for creating products without specific."""class Meta:model = Product- exclude = ['attributes', 'updated_at']+ exclude = ['updated_at']diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.pyindex 57117b0ef6..f316a9c524 100644--- a/saleor/dashboard/graphql/product/mutations.py+++ b/saleor/dashboard/graphql/product/mutations.py@@ -50,6 +50,7 @@ class ProductCreateMutation(ModelFormMutation):class Arguments:product_type_id = graphene.ID()category_id = graphene.ID()+ # TODO: add here optional attributes fieldclass Meta:form_class = ProductForm@@ -61,7 +62,6 @@ class ProductCreateMutation(ModelFormMutation):def get_form_kwargs(cls, root, info, **input):product_type_id = input.pop('product_type_id', None)category_id = input.pop('category_id', None)-product_type = get_node(info, product_type_id, only_type=ProductType)category = get_node(info, category_id, only_type=Category)kwargs = super().get_form_kwargs(root, info, **input)diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 75b7e579e5..ed55ab6390 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -11,6 +11,7 @@ from ..core.types import (CountableDjangoObjectType, Money, TaxedMoney, TaxedMoneyRange)from ..utils import get_nodefrom .filters import ProductFilterSet+from .fields import AttributeFieldclass ProductAvailability(graphene.ObjectType):@@ -95,9 +96,15 @@ class Category(CountableDjangoObjectType):return qs.distinct()+class ProductAttributes(graphene.ObjectType):+ name = graphene.String()+ value = graphene.String()++class ProductVariant(CountableDjangoObjectType):stock_quantity = graphene.Int()price_override = graphene.Field(Money)+ attributes = graphene.List(ProductAttributes)class Meta:model = models.ProductVariant@@ -106,6 +113,17 @@ class ProductVariant(CountableDjangoObjectType):def resolve_stock_quantity(self, info):return self.get_stock_quantity()+ def resolve_attributes(self, info):+ product_attributes = dict(+ models.ProductAttribute.objects.values_list('id', 'slug'))+ attribute_values = dict(+ models.AttributeChoiceValue.objects.values_list('id', 'slug'))+ attribute_list = []+ for k, v in self.attributes.items():+ name = product_attributes.get(int(k))+ value = attribute_values.get(int(v))+ attribute_list.append(ProductAttributes(name=name, value=value))+ return attribute_listclass ProductImage(CountableDjangoObjectType):url = graphene.String(size=graphene.String())