Skip to content

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

Add AvailableShippingMethods to the checkout

Pacu25cdd078c9ffd
diff --git a/Pipfile b/Pipfile
index 9a84234f17..f254ee1685 100644
--- a/Pipfile
+++ b/Pipfile
@@ -22,7 +22,7 @@ django-filter = ">=2.0"
django-fsm = ">=2.6.0"
django-graphql-jwt = ">=0.1.13"
django-impersonate = ">=1.3"
-django-measurement = "==3.0.0"
+django-measurement = "==3.1.1"
django-mptt = ">=0.7.1"
django-payments = ">=0.12.1"
django-phonenumber-field = ">=1.3.0"
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 49e689163c..abf242eb1b 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -4,6 +4,8 @@ from ...checkout import models
from ...core.utils.taxes import get_taxes_for_address
from ..core.types.common import CountableDjangoObjectType
from ..core.types.money import TaxedMoney
+from ..shipping.types import ShippingMethod
+from ..order.resolvers import resolve_shipping_methods
class CheckoutLine(CountableDjangoObjectType):
@@ -46,6 +48,9 @@ class Checkout(CountableDjangoObjectType):
CheckoutLine, description=(
'A list of checkout lines, each containing information about '
'an item in the checkout.'))
+ available_shipping_methods = graphene.List(
+ ShippingMethod, required=False,
+ description='Shipping methods that can be used with this order.')
class Meta:
description = 'Checkout object'
@@ -67,3 +72,9 @@ class Checkout(CountableDjangoObjectType):
def resolve_lines(self, info):
return self.lines.prefetch_related('variant')
+
+ def resolve_available_shipping_methods(self, info):
+ taxes = get_taxes_for_address(self.shipping_address)
+ price = self.get_subtotal(
+ taxes=taxes, discounts=info.context.discounts)
+ return resolve_shipping_methods(self, info, price.gross.amount)
diff --git a/saleor/graphql/order/resolvers.py b/saleor/graphql/order/resolvers.py
index d6aa97f1e6..ac63ed8b36 100644
--- a/saleor/graphql/order/resolvers.py
+++ b/saleor/graphql/order/resolvers.py
@@ -48,6 +48,18 @@ def resolve_order(info, id):
return None
+def resolve_shipping_methods(obj, info, price):
+ if not obj.is_shipping_required():
+ return None
+ if not obj.shipping_address:
+ return None
+
+ qs = shipping_models.ShippingMethod.objects
+ return qs.applicable_shipping_methods(
+ price=price, weight=obj.get_total_weight(),
+ country_code=obj.shipping_address.country.code)
+
+
def resolve_homepage_events(info):
# Filter only selected events to be displayed on homepage.
types = [
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index d883806dae..57d97e6dbd 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -261,12 +261,10 @@ class Order(CountableDjangoObjectType):
@staticmethod
def resolve_available_shipping_methods(obj, info):
- if not obj.is_shipping_required():
- return None
- if not obj.shipping_address:
- return None
- qs = shipping_models.ShippingMethod.objects
- qs = qs.applicable_shipping_methods(
- price=obj.get_subtotal().gross.amount, weight=obj.get_total_weight(),
- country_code=obj.shipping_address.country.code)
- return qs
+ from .resolvers import resolve_shipping_methods
+ return resolve_shipping_methods(
+ obj, info, obj.get_subtotal().gross.amount)
+
+ @staticmethod
+ def resolve_lines(obj, info):
+ return obj.lines.all()