Skip to content

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

Add default variant create to Product mutations

fowczarek18fae2c0b41b
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 3b671efac9..30f055b0a6 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1,6 +1,7 @@
from textwrap import dedent
import graphene
+from django.db import transaction
from django.template.defaultfilters import slugify
from graphene.types import InputObjectType
from graphql_jwt.decorators import permission_required
@@ -272,6 +273,16 @@ class ProductInput(graphene.InputObjectType):
seo = SeoInput(description='Search engine optimization fields.')
weight = WeightScalar(
description='Weight of the Product.', required=False)
+ sku = graphene.String(description=dedent("""Stock keeping unit. Note: this
+ field is required only used for products without variants."""))
+ quantity = graphene.Int(
+ description=dedent("""The total quantity of this variant availablefor
+ sale. Note: this field is only used for products without variants."""))
+ track_inventory = graphene.Boolean(
+ description=dedent("""Determines if the inventory of this variant
+ should be tracked. If false, the quantity won't change when customers
+ buy this item. Note: this field is only used for products
+ without variants."""))
class ProductCreateInput(ProductInput):
@@ -313,6 +324,21 @@ class ProductCreate(ModelMutation):
clean_seo_fields(cleaned_input)
return cleaned_input
+ @classmethod
+ @transaction.atomic
+ def save(cls, info, instance, cleaned_input):
+ instance.save()
+ if not instance.product_type.has_variants:
+ site_settings = info.context.site.settings
+ track_inventory = cleaned_input.get(
+ 'track_inventory', site_settings.track_inventory_by_default)
+ quantity = cleaned_input.get('quantity', 0)
+ sku = cleaned_input.get('sku')
+ variant = models.ProductVariant(
+ product=instance, track_inventory=track_inventory,
+ sku=sku, quantity=quantity)
+ variant.save()
+
@classmethod
def _save_m2m(cls, info, instance, cleaned_data):
collections = cleaned_data.get('collections', None)
@@ -335,6 +361,20 @@ class ProductUpdate(ProductCreate):
description = 'Updates an existing product.'
model = models.Product
+ @classmethod
+ @transaction.atomic
+ def save(cls, info, instance, cleaned_input):
+ instance.save()
+ if not instance.product_type.has_variants:
+ variant = instance.variants.first()
+ if 'track_inventory' in cleaned_input:
+ variant.track_inventory = cleaned_input.get('track_inventory')
+ if 'quantity' in cleaned_input:
+ variant.quantity = cleaned_input.get('quantity')
+ if 'sku' in cleaned_input:
+ variant.sku = cleaned_input.get('sku')
+ variant.save()
+
class ProductDelete(ModelDeleteMutation):
class Arguments: