Skip to content

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

Add available shipping methods to Order

Pacu2d7f57e4006ff
diff --git a/saleor/graphql/order/resolvers.py b/saleor/graphql/order/resolvers.py
index d7ac9cbef5..bfd1279521 100644
--- a/saleor/graphql/order/resolvers.py
+++ b/saleor/graphql/order/resolvers.py
@@ -1,6 +1,7 @@
import graphene
from ...order import models
+from ...shipping import models as shipping_models
from ..utils import filter_by_query_param
from .types import Order
@@ -25,3 +26,15 @@ def resolve_order(info, id):
if user.has_perm('order.manage_orders') or order.user == user:
return order
return None
+
+
+def resolve_shipping_methods(obj, info):
+ 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=obj.get_subtotal().gross.amount, weight=obj.get_total_weight(),
+ country_code=obj.shipping_address.country.code)
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index bc10de896e..521323963a 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -7,6 +7,7 @@ from ...product.templatetags.product_images import get_thumbnail
from ..account.types import User
from ..core.types.common import CountableDjangoObjectType
from ..core.types.money import Money, TaxedMoney
+from ..shipping.types import ShippingMethod
OrderEventsEnum = graphene.Enum.from_enum(OrderEvents)
OrderEventsEmailsEnum = graphene.Enum.from_enum(OrderEventsEmails)
@@ -105,6 +106,9 @@ class Order(CountableDjangoObjectType):
description='List of events associated with the order.')
user_email = graphene.String(
required=False, description='Email address of the customer.')
+ available_shipping_methods = graphene.List(
+ ShippingMethod, required=False,
+ description='Shipping methods that can be used with this order.')
class Meta:
description = 'Represents an order in the shop.'
@@ -166,6 +170,11 @@ class Order(CountableDjangoObjectType):
return obj.user.email
return None
+ @staticmethod
+ def resolve_available_shipping_methods(obj, info):
+ from .resolvers import resolve_shipping_methods
+ return resolve_shipping_methods(obj, info)
+
class OrderLine(CountableDjangoObjectType):
thumbnail_url = graphene.String(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 925b272997..37571d4003 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1562,7 +1562,7 @@ input SetPasswordInput {
type ShippingMethod implements Node {
id: ID!
name: String!
- type: ShippingMethodType!
+ type: ShippingMethodTypeEnum!
price: Money
minimumOrderPrice: Money
maximumOrderPrice: Money
@@ -1570,26 +1570,11 @@ type ShippingMethod implements Node {
maximumOrderWeight: Weight
}
-type ShippingMethodConnection {
- pageInfo: PageInfo!
- edges: [ShippingMethodEdge]!
-}
-
-type ShippingMethodEdge {
- node: ShippingMethod
- cursor: String!
-}
-
-enum ShippingMethodType {
+enum ShippingMethodTypeEnum {
PRICE
WEIGHT
}
-enum ShippingMethodTypeEnum {
- PRICE_BASED
- WEIGHT_BASED
-}
-
type ShippingPriceCreate {
errors: [Error]
shippingMethod: ShippingMethod
@@ -1620,7 +1605,7 @@ type ShippingZone implements Node {
id: ID!
name: String!
countries: [String]
- shippingMethods(before: String, after: String, first: Int, last: Int): ShippingMethodConnection
+ shippingMethods: [ShippingMethod]
priceRange: MoneyRange
}
diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.py
index 2d753ee652..ca651627fc 100644
--- a/saleor/graphql/shipping/types.py
+++ b/saleor/graphql/shipping/types.py
@@ -11,16 +11,42 @@ from ...shipping import ShippingMethodType, models
from ..core.types.common import CountableDjangoObjectType
from ..core.types.money import MoneyRange
+ShippingMethodTypeEnum = graphene.Enum(
+ 'ShippingMethodTypeEnum',
+ [(code.upper(), code) for code, name in ShippingMethodType.CHOICES])
+
+
+class ShippingMethod(DjangoObjectType):
+ type = ShippingMethodTypeEnum(
+ required=True, description='Type of the shipping method.')
+
+ class Meta:
+ description = """
+ Shipping method are the methods you'll use to get
+ customer's orders to them.
+ They are directly exposed to the customers."""
+ model = models.ShippingMethod
+ interfaces = [relay.Node]
+ exclude_fields = ['shipping_zone', 'orders']
+
class ShippingZone(CountableDjangoObjectType):
price_range = graphene.Field(
- MoneyRange, description="Lowest and highest prices for the shipping.")
+ MoneyRange, description='Lowest and highest prices for the shipping.')
countries = graphene.List(
graphene.String,
- description="List of countries available for the method.")
+ description='List of countries available for the method.')
+ shipping_methods = graphene.List(
+ ShippingMethod,
+ description=(
+ 'List of shipping methods available for orders'
+ ' shipped to countries within this shipping zone.'))
class Meta:
- description = 'Represents a shipping zone in the shop.'
+ description = """
+ Represents a shipping zone in the shop. Zones are the concept
+ used only for groupping shipping methods in the dashboard,
+ and are never exposed to the customers directly."""
model = models.ShippingZone
interfaces = [relay.Node]
filter_fields = {
@@ -34,6 +60,8 @@ class ShippingZone(CountableDjangoObjectType):
def resolve_countries(self, info):
return self.countries
+ def resolve_shipping_methods(self, info):
+ return self.shipping_methods.all()
class WeightScalar(Scalar):
@@ -61,17 +89,3 @@ class WeightScalar(Scalar):
@staticmethod
def parse_literal(node):
return node
-
-
-class ShippingMethod(DjangoObjectType):
-
- class Meta:
- description = 'Shipping method within a shipping zone.'
- model = models.ShippingMethod
- interfaces = [relay.Node]
- exclude_fields = ['shipping_zone', 'orders']
-
-
-class ShippingMethodTypeEnum(graphene.Enum):
- PRICE_BASED = ShippingMethodType.PRICE_BASED
- WEIGHT_BASED = ShippingMethodType.WEIGHT_BASED