Skip to content

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

Fixed invalid handling of nulls values in product variant prices

NyanKiyoshi8f4560c7d7e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3539d8519a..04eeb5776d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -71,6 +71,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Added validations for minimum password length in settings - #4735 by @fowczarek
- Add error codes to mutations responses - #4676 by @Kwaidan00
- Payment gateways are now saleor plugins with dynamic configuration - #4669 by @salwator
+- Fixed the inability of users to set a variant's `priceOverride` and `costPrice` to `null` - #4754 by @NyanKiyoshi
## 2.8.0
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 6df23a50c4..9663bbaaf6 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -1036,13 +1036,11 @@ class ProductVariantCreate(ModelMutation):
def clean_input(cls, info, instance: models.ProductVariant, data: dict):
cleaned_input = super().clean_input(info, instance, data)
- cost_price_amount = cleaned_input.pop("cost_price", None)
- if cost_price_amount is not None:
- cleaned_input["cost_price_amount"] = cost_price_amount
+ if "cost_price" in cleaned_input:
+ cleaned_input["cost_price_amount"] = cleaned_input.pop("cost_price")
- price_override_amount = cleaned_input.pop("price_override", None)
- if price_override_amount is not None:
- cleaned_input["price_override_amount"] = price_override_amount
+ if "price_override" in cleaned_input:
+ cleaned_input["price_override_amount"] = cleaned_input.pop("price_override")
# Attributes are provided as list of `AttributeValueInput` objects.
# We need to transform them into the format they're stored in the
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 013c2184ff..96c0f9f332 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -1,5 +1,6 @@
import graphene
import pytest
+from graphene.utils.str_converters import to_camel_case
from tests.api.utils import get_graphql_content
@@ -337,6 +338,66 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
assert data["sku"] == sku
[email protected]("field", ("cost_price", "price_override"))
+def test_update_product_variant_unset_amounts(
+ staff_api_client, product, permission_manage_products, field
+):
+ """Ensure setting nullable amounts to null is properly handled
+ (setting the amount to none) and doesn't override the currency.
+ """
+ query = """
+ mutation updateVariant (
+ $id: ID!,
+ $sku: String!,
+ $costPrice: Decimal,
+ $priceOverride: Decimal) {
+ productVariantUpdate(
+ id: $id,
+ input: {
+ sku: $sku,
+ costPrice: $costPrice,
+ priceOverride: $priceOverride
+ }) {
+ productVariant {
+ name
+ sku
+ quantity
+ costPrice {
+ currency
+ amount
+ localized
+ }
+ priceOverride {
+ currency
+ amount
+ localized
+ }
+ }
+ }
+ }
+
+ """
+ variant = product.variants.first()
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ sku = variant.sku
+
+ camel_case_field_name = to_camel_case(field)
+
+ variables = {"id": variant_id, "sku": sku, camel_case_field_name: None}
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ variant.refresh_from_db()
+
+ assert variant.currency is not None
+ assert getattr(variant, field) is None
+
+ content = get_graphql_content(response)
+ data = content["data"]["productVariantUpdate"]["productVariant"]
+ assert data[camel_case_field_name] is None
+
+
QUERY_UPDATE_VARIANT_ATTRIBUTES = """
mutation updateVariant (
$id: ID!,