Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Reimplement price for product but rename it to basePrice, and restrict it
NyanKiyoshia62dc3d30d28
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 095278bb42..3bc38dc0a9 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -239,15 +239,20 @@ class ProductVariant(CountableDjangoObjectType):"""Override the base price of a product if necessary.A value of `null` indicates that the default productprice is used."""))- price = graphene.Field(Money, description='Price of the product variant.')+ price = graphene.Field(+ Money, description='Price of the product variant.',+ deprecation_reason=(+ 'Has been replaced by \'pricing.price_undiscounted\''))availability = graphene.Field(VariantPricingInfo, description=dedent("""Informs about variant's availability in thestorefront, current price and discounted price."""),deprecation_reason='Has been renamed to \'pricing\'.')pricing = graphene.Field(- VariantPricingInfo, description=dedent("""Informs about product's pricing in the- storefront, current price and discounts."""))+ VariantPricingInfo,+ description=dedent(+ """Lists the storefront product's pricing,+ the current price and discounts, only meant for displaying"""))attributes = graphene.List(graphene.NonNull(SelectedAttribute), required=True,description='List of attributes assigned to this variant.')@@ -377,14 +382,17 @@ class Product(CountableDjangoObjectType):storefront, current price and discounts."""),deprecation_reason='Has been renamed to \'pricing\'.')pricing = graphene.Field(- ProductPricingInfo, description=dedent("""Informs about product's pricing in the- storefront, current price and discounts."""))+ ProductPricingInfo, description=dedent(+ """Lists the storefront product's pricing,+ the current price and discounts, only meant for displaying."""))+ base_price = graphene.Field(+ Money,+ description='The product\'s default base price.')price = graphene.Field(Money,- description=dedent("""The product's base price (without any discounts- applied)."""),+ description='The product\'s default base price.',deprecation_reason=(- 'Has been replaced by \'pricing.price_range_undiscounted\''))+ 'Has been replaced by \'basePrice\''))tax_rate = TaxRateType(description='A type of tax rate.')attributes = graphene.List(graphene.NonNull(SelectedAttribute), required=True,@@ -465,6 +473,12 @@ class Product(CountableDjangoObjectType):resolve_availability = resolve_pricing+ @permission_required('product.manage_products')+ def resolve_price(self, _info):+ return self.price++ resolve_base_price = resolve_price+@gql_optimizer.resolver_hints(prefetch_related='product_type__product_attributes__values')def resolve_attributes(self, *_args):diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.pyindex 2fc721f0ea..f6ad192a16 100644--- a/tests/api/test_graphql.py+++ b/tests/api/test_graphql.py@@ -110,12 +110,6 @@ def test_real_query(user_api_client, product):fragment ProductFragmentQuery on Product {idname- price {- amount- currency- localized- __typename- }pricing {...ProductPriceFragmentQuery__typenamediff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 7b64efa196..768923580d 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -393,8 +393,14 @@ def test_sort_products(user_api_client, product):products(sortBy: %(sort_by_product_order)s, first: 2) {edges {node {- price {- amount+ pricing {+ priceRangeUndiscounted {+ start {+ gross {+ amount+ }+ }+ }}updatedAt}@@ -403,25 +409,23 @@ def test_sort_products(user_api_client, product):}"""+ def _get_node_price(data, node):+ return (+ data['data']['products']['edges'][node]['node']['pricing'][+ 'priceRangeUndiscounted']['start']['gross']['amount'])+asc_price_query = query % {'sort_by_product_order': '{field: PRICE, direction:ASC}'}response = user_api_client.post_graphql(asc_price_query)content = get_graphql_content(response)- price_0 = content['data']['products']['edges'][0]['node']['price'][- 'amount']- price_1 = content['data']['products']['edges'][1]['node']['price'][- 'amount']- assert price_0 < price_1++ assert _get_node_price(content, 0) < _get_node_price(content, 1)desc_price_query = query % {'sort_by_product_order': '{field: PRICE, direction:DESC}'}response = user_api_client.post_graphql(desc_price_query)content = get_graphql_content(response)- price_0 = content['data']['products']['edges'][0]['node']['price'][- 'amount']- price_1 = content['data']['products']['edges'][1]['node']['price'][- 'amount']- assert price_0 > price_1+ assert _get_node_price(content, 0) > _get_node_price(content, 1)asc_date_query = query % {'sort_by_product_order': '{field: DATE, direction:ASC}'}@@ -1518,7 +1522,7 @@ def test_product_update_variants_names(mock__update_variants_names,variant_attributes = [product_type.variant_attributes.first()]variant_attr_ids = [attr.pk for attr in variant_attributes]update_variants_names(product_type.pk, variant_attr_ids)- mock__update_variants_names.call_count == 1+ assert mock__update_variants_names.call_count == 1def test_product_variants_by_ids(user_api_client, variant):@@ -1846,3 +1850,28 @@ def test_bulk_unpublish_products(assert content['data']['productBulkPublish']['count'] == len(product_list)assert not any(product.is_published for product in product_list)+++def test_product_base_price_permission(+ staff_api_client, permission_manage_products, product):+ query = """+ query basePrice($productID: ID!) {+ product(id: $productID) {+ basePrice {+ amount+ }+ }+ }+ """+ product_id = graphene.Node.to_global_id('Product', product.id)++ variables = {'productID': product_id}+ permissions = [permission_manage_products]++ response = staff_api_client.post_graphql(query, variables, permissions)+ content = get_graphql_content(response)++ assert 'basePrice' in content['data']['product']+ assert (+ content['data']['product']['basePrice'][+ 'amount'] == product.price.amount)