Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use basePrice in product mutations
Marcin Gębala18d51283e63d
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex bb4977042e..2d2f1b3606 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -270,7 +270,11 @@ class ProductInput(graphene.InputObjectType):description="Determines if product is visible to customers.")name = graphene.String(description="Product name.")- price = Decimal(description="Product price.")+ price = Decimal(+ description="""+ Product price. Note: this field is deprecated, use basePrice instead."""+ )+ base_price = Decimal(description="Product price.")tax_rate = TaxRateType(description="Tax rate.")seo = SeoInput(description="Search engine optimization fields.")weight = WeightScalar(description="Weight of the Product.", required=False)@@ -323,6 +327,12 @@ class ProductCreate(ModelMutation):instance.product_type if instance.pk else cleaned_input.get("product_type"))+ # Try to get price from "basePrice" or "price" field. Once "price" is removed+ # from the schema, only "basePrice" should be used here.+ price = data.get("base_price", data.get("price"))+ if price is not None:+ cleaned_input["price"] = price+if attributes and product_type:qs = product_type.product_attributes.prefetch_related("values")try:diff --git a/saleor/product/models.py b/saleor/product/models.pyindex d8a7667f26..8dc7423d39 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -279,7 +279,11 @@ class ProductVariant(models.Model):@propertydef base_price(self):- return self.price_override or self.product.price+ return (+ self.price_override+ if self.price_override is not None+ else self.product.price+ )def get_price(self, discounts=None, taxes=None):price = calculate_discounted_price(self.product, self.base_price, discounts)diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex df6e7eb151..2bb9f5379a 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -146,9 +146,6 @@ def test_product_query(staff_api_client, product, permission_manage_products):stockQuantity}isAvailable- price {- amount- }pricing {available,priceRange {@@ -205,7 +202,6 @@ def test_product_query(staff_api_client, product, permission_manage_products):assert purchase_cost.stop.amount == product_data["purchaseCost"]["stop"]["amount"]assert product_data["isAvailable"] is product.is_visibleassert product_data["pricing"]["available"] is product.is_visible- assert product_data["price"]["amount"] == product.price.amountassert margin[0] == product_data["margin"]["start"]assert margin[1] == product_data["margin"]["stop"]@@ -679,7 +675,7 @@ def test_create_product($isPublished: Boolean!,$chargeTaxes: Boolean!,$taxRate: TaxRateType!,- $price: Decimal!,+ $basePrice: Decimal!,$attributes: [AttributeValueInput!]) {productCreate(input: {@@ -691,7 +687,7 @@ def test_create_product(isPublished: $isPublished,chargeTaxes: $chargeTaxes,taxRate: $taxRate,- price: $price,+ basePrice: $basePrice,attributes: $attributes}) {product {@@ -704,7 +700,7 @@ def test_create_product(chargeTaxestaxRatename- price {+ basePrice {amount}productType {@@ -757,7 +753,7 @@ def test_create_product("isPublished": product_is_published,"chargeTaxes": product_charge_taxes,"taxRate": product_tax_rate,- "price": product_price,+ "basePrice": product_price,"attributes": [{"slug": color_attr_slug, "value": color_value_slug},{"slug": size_attr_slug, "value": non_existent_attr_value},@@ -792,7 +788,7 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """$categoryId: ID!$name: String!,$description: String!,- $price: Decimal!,+ $basePrice: Decimal!,$sku: String,$quantity: Int,$trackInventory: Boolean)@@ -803,7 +799,7 @@ QUERY_CREATE_PRODUCT_WITHOUT_VARIANTS = """productType: $productTypeId,name: $name,description: $description,- price: $price,+ basePrice: $basePrice,sku: $sku,quantity: $quantity,trackInventory: $trackInventory@@ -854,7 +850,7 @@ def test_create_product_without_variants("categoryId": category_id,"name": product_name,"description": product_description,- "price": product_price,+ "basePrice": product_price,"sku": sku,"quantity": quantity,"trackInventory": track_inventory,@@ -893,7 +889,7 @@ def test_create_product_without_variants_sku_validation("categoryId": category_id,"name": product_name,"description": product_description,- "price": product_price,+ "basePrice": product_price,"sku": None,"quantity": quantity,"trackInventory": track_inventory,@@ -932,7 +928,7 @@ def test_create_product_without_variants_sku_duplication("categoryId": category_id,"name": product_name,"description": product_description,- "price": product_price,+ "basePrice": product_price,"sku": sku,"quantity": quantity,"trackInventory": track_inventory,@@ -954,7 +950,7 @@ def test_product_create_without_product_type(mutation createProduct($categoryId: ID!) {productCreate(input: {name: "Product",- price: "2.5",+ basePrice: "2.5",productType: "",category: $categoryId}) {product {@@ -994,7 +990,7 @@ def test_update_product($isPublished: Boolean!,$chargeTaxes: Boolean!,$taxRate: TaxRateType!,- $price: Decimal!,+ $basePrice: Decimal!,$attributes: [AttributeValueInput!]) {productUpdate(id: $productId,@@ -1005,7 +1001,7 @@ def test_update_product(isPublished: $isPublished,chargeTaxes: $chargeTaxes,taxRate: $taxRate,- price: $price,+ basePrice: $basePrice,attributes: $attributes}) {product {@@ -1017,7 +1013,7 @@ def test_update_product(chargeTaxestaxRatename- price {+ basePrice {amount}productType {@@ -1056,7 +1052,7 @@ def test_update_product("isPublished": product_isPublished,"chargeTaxes": product_chargeTaxes,"taxRate": product_taxRate,- "price": product_price,+ "basePrice": product_price,}response = staff_api_client.post_graphql(@@ -1860,8 +1856,12 @@ def test_product_variant_price(query getProductVariants($id: ID!) {product(id: $id) {variants {- price {- amount+ pricing {+ priceUndiscounted {+ gross {+ amount+ }+ }}}}@@ -1872,7 +1872,7 @@ def test_product_variant_price(response = user_api_client.post_graphql(query, variables)content = get_graphql_content(response)data = content["data"]["product"]- variant_price = data["variants"][0]["price"]+ variant_price = data["variants"][0]["pricing"]["priceUndiscounted"]["gross"]assert variant_price["amount"] == api_variant_pricediff --git a/tests/test_product.py b/tests/test_product.pyindex ae0457fb68..5ce41eac20 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -638,11 +638,12 @@ def test_product_get_price_range_do_not_charge_taxes(assert price == TaxedMoneyRange(start=expected_price, stop=expected_price)-def test_variant_base_price(product):[email protected]("price_override", ["15.00", "0.00"])+def test_variant_base_price(product, price_override):variant = product.variants.get()assert variant.base_price == product.price- variant.price_override = Money("15.00", "USD")+ variant.price_override = Money(price_override, "USD")variant.save()assert variant.base_price == variant.price_override