Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Expose totals in chekcout API
Artur Smętecb73fb490a7
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.pyindex 48d1a4236f..1e4adfe1dc 100644--- a/saleor/graphql/checkout/types.py+++ b/saleor/graphql/checkout/types.py@@ -1,18 +1,48 @@-from ..core.types.common import CountableDjangoObjectType+import graphene+from ...checkout import models-from ...product.models import Product-from graphene import relay+from ..core.types.common import CountableDjangoObjectType+from ..core.types.money import TaxedMoney+from ...core.utils.taxes import get_taxes_for_address+class CheckoutLine(CountableDjangoObjectType):+ total_price = graphene.Field(TaxedMoney, description='Total price')+ requires_shipping = graphene.Boolean(description='Line requires shipping')+class Meta:exclude_fields = ['cart', 'data']description = 'Represents user address data.'- interfaces = [relay.Node]+ interfaces = [graphene.relay.Node]model = models.CartLine+ def resolve_total_price(self, info):+ taxes = get_taxes_for_address(self.cart.shipping_address)+ return self.get_total(taxes=taxes)++ def resolve_requires_shipping(self, info):+ return self.is_shipping_required()+class Checkout(CountableDjangoObjectType):+ total_price = graphene.Field(TaxedMoney, description='Total price')+ subtotal_price = graphene.Field(+ TaxedMoney, description='Total without shipping')+ shipping_price = graphene.Field(TaxedMoney, description='Shipping price')+class Meta:description = 'Checkout object'model = models.Cart- interfaces = [relay.Node]+ interfaces = [graphene.relay.Node]++ def resolve_total_price(self, info):+ taxes = get_taxes_for_address(self.shipping_address)+ return self.get_total(taxes=taxes)++ def resolve_subtotal_price(self, info):+ taxes = get_taxes_for_address(self.shipping_address)+ return self.get_subtotal(taxes=taxes)++ def resolve_shipping_price(self, info):+ taxes = get_taxes_for_address(self.shipping_address)+ return self.get_shipping_price(taxes=taxes)diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 0a1399bcee..ec8ec613ef 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -491,3 +491,49 @@ def test_fetch_checkout_invalid_token(user_api_client):assert 'errors' not in contentdata = content['data']['checkout']assert data is None+++def test_checkout_prices(user_api_client, cart_with_item):+ query = """+ query getCheckout($token: UUID!) {+ checkout(token: $token) {+ token,+ totalPrice {+ currency+ gross {+ amount+ }+ }+ subtotalPrice {+ currency+ gross {+ amount+ }+ }+ lines {+ edges {+ node {+ totalPrice {+ currency+ gross {+ amount+ }+ }+ }+ }+ }+ }+ }+ """+ variables = json.dumps({+ 'token': str(cart_with_item.token)+ })+ response = user_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['checkout']+ assert data['token'] == str(cart_with_item.token)+ assert len(data['lines']) == cart_with_item.lines.count()+ assert data['totalPrice']['gross']['amount'] == cart_with_item.get_total().gross.amount # noqa+ assert data['subtotalPrice']['gross']['amount'] == cart_with_item.get_subtotal().gross.amount # noqa