Skip to content

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

Update ProductVariantCreate and ProductCreate

IKarbowiak8a8850bdcb0c
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 861b1a76d8..02dcd6bc6a 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -525,14 +525,6 @@ class ProductInput(graphene.InputObjectType):
"a product doesn't use variants."
)
)
- stocks = graphene.List(
- graphene.NonNull(StockInput),
- description=(
- "Stocks of a product available for sale. Note: this field is "
- "only used if a product doesn't use variants."
- ),
- required=False,
- )
quantity = graphene.Int(
description=(
"The total quantity of a product available for sale. Note: this field is "
@@ -557,6 +549,14 @@ class ProductCreateInput(ProductInput):
name="productType",
required=True,
)
+ stocks = graphene.List(
+ graphene.NonNull(StockInput),
+ description=(
+ "Stocks of a product available for sale. Note: this field is "
+ "only used if a product doesn't use variants."
+ ),
+ required=False,
+ )
T_INPUT_MAP = List[Tuple[models.Attribute, List[str]]]
@@ -923,8 +923,11 @@ class ProductCreate(ModelMutation):
product=instance, track_inventory=track_inventory, sku=sku
)
stocks = cleaned_input.get("stocks")
+ quantity = cleaned_input.get("quantity")
if stocks:
cls.create_variant_stocks(variant, stocks)
+ elif quantity: # DEPRECATED: Will be removed in 2.11
+ set_stock_quantity(variant, info.context.country, quantity)
attributes = cleaned_input.get("attributes")
if attributes:
@@ -996,6 +999,7 @@ class ProductUpdate(ProductCreate):
if "track_inventory" in cleaned_input:
variant.track_inventory = cleaned_input["track_inventory"]
update_fields.append("track_inventory")
+ # DEPRECATED: Wil be removed in 2.11. Use ProductVariantStocksUpdate insted.
if "quantity" in cleaned_input:
quantity = cleaned_input.get("quantity")
set_stock_quantity(variant, info.context.country, quantity)
@@ -1074,7 +1078,10 @@ class ProductVariantInput(graphene.InputObjectType):
price_override = Decimal(description="Special price of the particular variant.")
sku = graphene.String(description="Stock keeping unit.")
quantity = graphene.Int(
- description="The total quantity of this variant available for sale."
+ description="The total quantity of this variant available for sale.",
+ deprecation_reason=(
+ "DEPRECATED: Will be removed in 2.11. Use stocks input field instead."
+ ),
)
track_inventory = graphene.Boolean(
description=(
@@ -1096,6 +1103,11 @@ class ProductVariantCreateInput(ProductVariantInput):
name="product",
required=True,
)
+ stocks = graphene.List(
+ graphene.NonNull(StockInput),
+ description=("Stocks of a product available for sale."),
+ required=False,
+ )
class ProductVariantCreate(ModelMutation):
@@ -1208,8 +1220,11 @@ class ProductVariantCreate(ModelMutation):
instance.save()
# Recalculate the "minimal variant price" for the parent product
update_product_minimal_variant_price_task.delay(instance.product_id)
+ stocks = cleaned_input.get("stocks")
quantity = cleaned_input.get("quantity")
- if quantity is not None:
+ if stocks:
+ cls.create_variant_stocks(instance, stocks)
+ elif quantity: # DEPRECATED: Will be removed in 2.11
set_stock_quantity(instance, info.context.country, quantity)
attributes = cleaned_input.get("attributes")
@@ -1218,6 +1233,18 @@ class ProductVariantCreate(ModelMutation):
instance.name = generate_name_for_variant(instance)
instance.save(update_fields=["name"])
+ @classmethod
+ def create_variant_stocks(cls, variant, stocks):
+ warehouse_ids = [stock["warehouse"] for stock in stocks]
+ warehouses = cls.get_nodes_or_error(
+ warehouse_ids, "warehouse", only_type=Warehouse
+ )
+ try:
+ create_stocks(variant, stocks, warehouses)
+ except ValidationError as error:
+ error.code = StockErorrCode.UNIQUE
+ raise ValidationError({"warehouse": error})
+
class ProductVariantUpdate(ProductVariantCreate):
class Arguments:
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 14f16a46ea..7a2a60e586 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3214,10 +3214,10 @@ input ProductCreateInput {
seo: SeoInput
weight: WeightScalar
sku: String
- stocks: [StockInput!]
quantity: Int
trackInventory: Boolean
productType: ID!
+ stocks: [StockInput!]
}
type ProductDelete {
@@ -3327,7 +3327,6 @@ input ProductInput {
seo: SeoInput
weight: WeightScalar
sku: String
- stocks: [StockInput!]
quantity: Int
trackInventory: Boolean
}
@@ -3614,6 +3613,7 @@ input ProductVariantCreateInput {
trackInventory: Boolean
weight: WeightScalar
product: ID!
+ stocks: [StockInput!]
}
type ProductVariantDelete {
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 376c7786d3..0c76ec0eff 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -69,12 +69,13 @@ def test_fetch_variant(staff_api_client, product, permission_manage_products):
def test_create_variant(
- staff_api_client, product, product_type, permission_manage_products
+ staff_api_client, product, product_type, permission_manage_products, warehouse
):
query = """
mutation createVariant (
$productId: ID!,
$sku: String!,
+ $stocks: [StockInput!],
$priceOverride: Decimal,
$costPrice: Decimal,
$attributes: [AttributeValueInput]!,
@@ -84,6 +85,7 @@ def test_create_variant(
input: {
product: $productId,
sku: $sku,
+ stocks: $stocks,
priceOverride: $priceOverride,
costPrice: $costPrice,
attributes: $attributes,
@@ -119,6 +121,12 @@ def test_create_variant(
value
unit
}
+ stocks {
+ quantity
+ warehouse {
+ slug
+ }
+ }
}
}
}
@@ -128,18 +136,23 @@ def test_create_variant(
sku = "1"
price_override = 1.32
cost_price = 3.22
- quantity = 10
weight = 10.22
variant_slug = product_type.variant_attributes.first().slug
variant_id = graphene.Node.to_global_id(
"Attribute", product_type.variant_attributes.first().pk
)
variant_value = "test-value"
+ stocks = [
+ {
+ "warehouse": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ "quantity": 20,
+ }
+ ]
variables = {
"productId": product_id,
"sku": sku,
- "quantity": quantity,
+ "stocks": stocks,
"costPrice": cost_price,
"priceOverride": price_override,
"weight": weight,
@@ -160,6 +173,9 @@ def test_create_variant(
assert data["attributes"][0]["values"][0]["slug"] == variant_value
assert data["weight"]["unit"] == "kg"
assert data["weight"]["value"] == weight
+ assert len(data["stocks"]) == 1
+ assert data["stocks"][0]["quantity"] == stocks[0]["quantity"]
+ assert data["stocks"][0]["warehouse"]["slug"] == warehouse.slug
def test_create_product_variant_not_all_attributes(