Skip to content

Commit

Permalink
Merge pull request #4754 from NyanKiyoshi/fix/variants/unset-prices
Browse files Browse the repository at this point in the history
Fixed invalid handling of nulls values in product variant prices
  • Loading branch information
maarcingebala committed Sep 25, 2019
2 parents 2665f58 + 9b00786 commit 48d3bff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Added support for sorting product by their attribute values through given attribute ID - #4740 by @NyanKiyoshi
- Unified MenuItemMove to other reordering mutations. It now uses relative positions instead of absolute ones (breaking change) - #4734 by @NyanKiyoshi.
- Add descriptions for queries and query arguments - #4758 by @maarcingebala
- Fixed the inability of users to set a variant's `priceOverride` and `costPrice` to `null` - #4754 by @NyanKiyoshi

## 2.8.0

Expand Down
10 changes: 4 additions & 6 deletions saleor/graphql/product/mutations/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,11 @@ def clean_attributes(
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
Expand Down
61 changes: 61 additions & 0 deletions tests/api/test_variant.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -337,6 +338,66 @@ def test_update_product_variant(staff_api_client, product, permission_manage_pro
assert data["sku"] == sku


@pytest.mark.parametrize("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!,
Expand Down

0 comments on commit 48d3bff

Please sign in to comment.