Skip to content

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

Use frozen order data to return shipping method (#8688)

Adam Bogdał1f6a975cab95
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index ee645d971b..16977cee34 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -17,6 +17,7 @@ from prices import Money, TaxedMoney
from ....account.models import CustomerEvent
from ....checkout import AddressType
+from ....checkout.utils import PRIVATE_META_APP_SHIPPING_ID
from ....core.anonymize import obfuscate_email
from ....core.notify_events import NotifyEventType
from ....core.prices import quantize_price
@@ -355,6 +356,12 @@ query OrdersQuery {
}
shippingMethod{
id
+ name
+ price {
+ amount
+ currency
+ }
+
}
deliveryMethod {
__typename
@@ -475,6 +482,34 @@ def test_order_query_shipping_method_channel_listing_does_not_exist(
)
+def test_order_query_external_shipping_method(
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+):
+ external_shipping_method_id = graphene.Node.to_global_id("app", "1:external123")
+
+ # given
+ order = order_with_lines
+ order.shipping_method = None
+ order.private_metadata = {PRIVATE_META_APP_SHIPPING_ID: external_shipping_method_id}
+ order.save()
+
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ # when
+ response = staff_api_client.post_graphql(ORDERS_QUERY)
+ content = get_graphql_content(response)
+
+ # then
+ order_data = content["data"]["orders"]["edges"][0]["node"]
+ assert order_data["shippingMethod"]["id"] == external_shipping_method_id
+ assert order_data["shippingMethod"]["name"] == order.shipping_method_name
+ assert order_data["shippingMethod"]["price"]["amount"] == float(
+ order.shipping_price_gross.amount
+ )
+
+
def test_order_discounts_query(
staff_api_client,
permission_manage_orders,
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 430157f10a..3cc90bbf6a 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -43,6 +43,7 @@ from ...payment.model_helpers import (
from ...product import ProductMediaTypes
from ...product.models import ALL_PRODUCTS_PERMISSIONS
from ...product.product_images import get_product_image_thumbnail
+from ...shipping.interface import ShippingMethodData
from ..account.dataloaders import AddressByIdLoader, UserByUserIdLoader
from ..account.types import User
from ..account.utils import requestor_has_access
@@ -1056,15 +1057,14 @@ class Order(CountableDjangoObjectType):
external_app_shipping_id = get_external_shipping_id(root)
if external_app_shipping_id:
- shipping_method = info.context.plugins.get_shipping_method(
- checkout=root,
- channel_slug=root.channel.slug,
- shipping_method_id=external_app_shipping_id,
+ keep_gross = info.context.site.settings.include_taxes_in_prices
+ price = root.shipping_price_gross if keep_gross else root.shipping_price_net
+ method = ShippingMethodData(
+ id=external_app_shipping_id,
+ name=root.shipping_method_name,
+ price=price,
)
- if shipping_method:
- return ChannelContext(
- node=shipping_method, channel_slug=root.channel.slug
- )
+ return ChannelContext(node=method, channel_slug=root.channel.slug)
if not root.shipping_method_id:
return None
@@ -1084,7 +1084,7 @@ class Order(CountableDjangoObjectType):
@classmethod
def resolve_delivery_method(cls, root: models.Order, info):
- if root.shipping_method_id:
+ if root.shipping_method_id or get_external_shipping_id(root):
return cls.resolve_shipping_method(root, info)
if root.collection_point_id:
collection_point = WarehouseByIdLoader(info.context).load(
diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.py
index 2fdb6a8acc..9d5733c4de 100644
--- a/saleor/graphql/shipping/types.py
+++ b/saleor/graphql/shipping/types.py
@@ -156,7 +156,11 @@ class ShippingMethod(ChannelContextTypeWithMetadataForObjectType):
info.context
)
.load((root.node.id, root.channel_slug))
- .then(lambda channel_listing: channel_listing.price)
+ .then(
+ lambda channel_listing: channel_listing.price
+ if channel_listing
+ else None
+ )
)
@staticmethod
diff --git a/saleor/shipping/interface.py b/saleor/shipping/interface.py
index 830784c865..af280fc0a6 100644
--- a/saleor/shipping/interface.py
+++ b/saleor/shipping/interface.py
@@ -16,8 +16,8 @@ class ShippingMethodData:
"""Dataclass for storing information about a shipping method."""
id: str
- name: str
price: Optional[Money]
+ name: Optional[str] = None
description: Optional[str] = None
type: Optional[str] = None
maximum_order_price: Optional[Money] = None