Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add available shipping methods to Order
Pacu2d7f57e4006ff
diff --git a/saleor/graphql/order/resolvers.py b/saleor/graphql/order/resolvers.pyindex d7ac9cbef5..bfd1279521 100644--- a/saleor/graphql/order/resolvers.py+++ b/saleor/graphql/order/resolvers.py@@ -1,6 +1,7 @@import graphenefrom ...order import models+from ...shipping import models as shipping_modelsfrom ..utils import filter_by_query_paramfrom .types import Order@@ -25,3 +26,15 @@ def resolve_order(info, id):if user.has_perm('order.manage_orders') or order.user == user:return orderreturn 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.pyindex 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_thumbnailfrom ..account.types import Userfrom ..core.types.common import CountableDjangoObjectTypefrom ..core.types.money import Money, TaxedMoney+from ..shipping.types import ShippingMethodOrderEventsEnum = 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.emailreturn 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.graphqlindex 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: MoneyminimumOrderPrice: MoneymaximumOrderPrice: 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 {PRICEWEIGHT}-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.pyindex 2d753ee652..ca651627fc 100644--- a/saleor/graphql/shipping/types.py+++ b/saleor/graphql/shipping/types.py@@ -11,16 +11,42 @@ from ...shipping import ShippingMethodType, modelsfrom ..core.types.common import CountableDjangoObjectTypefrom ..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.ShippingZoneinterfaces = [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):@staticmethoddef 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