Skip to content

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

Fix/click and collect (#8143)

Paweł Kucharski63dea36f43f5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b5c30933a6..8ea538ec06 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Introduce gift card feature - #7827 by @IKarbowiak, @tomaszszymanski129
- Deprecate `setup_future_usage` from `checkoutComplete.paymentData` input - will be removed in Saleor 4.0 - #7994 by @mateuszgrzyb
- Possibility to pass metadata in input of `checkoutPaymentCreate` - #8076 by @mateuszgrzyb
+- Fix shipping address issue in `availableCollectionPoints` resolver for checkout - #8143 by @kuchichan
# 3.0.0 [Unreleased]
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index e2b2bc53e9..121984411a 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -246,7 +246,7 @@ def fetch_checkout_info(
checkout_info, shipping_address, lines, discounts, manager
)
valid_pick_up_points = get_valid_collection_points_for_checkout_info(
- checkout_info, shipping_address, lines
+ shipping_address, lines, checkout_info
)
checkout_info.valid_shipping_methods = valid_shipping_methods
checkout_info.valid_pick_up_points = valid_pick_up_points
@@ -297,15 +297,19 @@ def get_valid_shipping_method_list_for_checkout_info(
def get_valid_collection_points_for_checkout_info(
- checkout_info: "CheckoutInfo",
shipping_address: Optional["Address"],
lines: Iterable[CheckoutLineInfo],
+ checkout_info: CheckoutInfo,
):
from .utils import get_valid_collection_points_for_checkout
- country_code = shipping_address.country.code if shipping_address else None
+ if shipping_address:
+ country_code = shipping_address.country.code
+ else:
+ country_code = checkout_info.channel.default_country.code
+
valid_collection_points = get_valid_collection_points_for_checkout(
- lines, checkout_info, country_code=country_code, quantity_check=False
+ lines, country_code=country_code, quantity_check=False
)
return list(valid_collection_points)
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index f00660ed52..d746950f3e 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -611,7 +611,6 @@ def get_valid_shipping_methods_for_checkout(
def get_valid_collection_points_for_checkout(
lines: Iterable["CheckoutLineInfo"],
- checkout_info: "CheckoutInfo",
country_code: Optional[str] = None,
quantity_check: bool = True,
):
@@ -624,7 +623,7 @@ def get_valid_collection_points_for_checkout(
if not is_shipping_required(lines):
return []
- if not checkout_info.shipping_address:
+ if not country_code:
return []
line_ids = [line_info.line.id for line_info in lines]
lines = CheckoutLine.objects.filter(id__in=line_ids)
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 0d3df378f2..6494ac68f9 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -848,7 +848,7 @@ def create_product_sales(how_many=5):
yield "Sale: %s" % (sale,)
-def create_channel(channel_name, currency_code, slug=None):
+def create_channel(channel_name, currency_code, slug=None, country=None):
if not slug:
slug = slugify(channel_name)
channel, _ = Channel.objects.get_or_create(
@@ -857,6 +857,7 @@ def create_channel(channel_name, currency_code, slug=None):
"name": channel_name,
"currency_code": currency_code,
"is_active": True,
+ "default_country": country,
},
)
return f"Channel: {channel}"
@@ -867,11 +868,9 @@ def create_channels():
channel_name="Channel-USD",
currency_code="USD",
slug=settings.DEFAULT_CHANNEL_SLUG,
+ country=settings.DEFAULT_COUNTRY,
)
- yield create_channel(
- channel_name="Channel-PLN",
- currency_code="PLN",
- )
+ yield create_channel(channel_name="Channel-PLN", currency_code="PLN", country="PL")
def create_shipping_zone(shipping_methods_names, countries, shipping_zone_name):
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index b403d5fde4..a573364f76 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -90,7 +90,7 @@ def clean_delivery_method(
ERROR_DOES_NOT_SHIP, code=CheckoutErrorCode.SHIPPING_NOT_REQUIRED.value
)
- if not checkout_info.shipping_address:
+ if not checkout_info.shipping_address and isinstance(method, models.ShippingMethod):
raise ValidationError(
"Cannot choose a shipping method for a checkout without the "
"shipping address.",
@@ -512,7 +512,7 @@ class CheckoutLinesAdd(BaseMutation):
)
checkout_info.valid_pick_up_points = (
get_valid_collection_points_for_checkout_info(
- checkout_info, checkout_info.shipping_address, lines
+ checkout_info.shipping_address, lines, checkout_info
)
)
return lines
@@ -562,7 +562,7 @@ class CheckoutLinesAdd(BaseMutation):
)
checkout_info.valid_pick_up_points = (
get_valid_collection_points_for_checkout_info(
- checkout_info, checkout_info.shipping_address, lines
+ checkout_info.shipping_address, lines, checkout_info
)
)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index bf89db53fd..a06a1f164e 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -1547,6 +1547,26 @@ def test_checkout_avail_collect_points_returns_empty_list_when_not_in_shipping_z
assert not data["availableCollectionPoints"]
+def test_checkout_avail_collect_fallbacks_to_channel_country_when_no_shipping_address(
+ api_client, warehouse_for_cc, checkout_with_items_for_cc
+):
+ query = GET_CHECKOUT_AVAILABLE_COLLECTION_POINTS
+ checkout_with_items_for_cc.shipping_address = None
+ checkout_with_items_for_cc.save()
+
+ variables = {"token": checkout_with_items_for_cc.token}
+ response = api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkout"]
+
+ assert data["availableCollectionPoints"] == [
+ {
+ "address": {"streetAddress1": warehouse_for_cc.address.street_address_1},
+ "name": warehouse_for_cc.name,
+ }
+ ]
+
+
def test_create_checkout_with_unpublished_product(
user_api_client, checkout_with_item, stock, channel_USD
):
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 9700d8e224..f62771f6ed 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -491,20 +491,26 @@ class Checkout(CountableDjangoObjectType):
@traced_resolver
def resolve_available_collection_points(root: models.Checkout, info):
def get_available_collection_points(data):
- address, lines, checkout_info = data
+ address, lines, channel = data
+
+ if address:
+ country_code = address.country.code
+ else:
+ country_code = channel.default_country.code
+
return get_valid_collection_points_for_checkout(
- lines, checkout_info, country_code=address.country.code
+ lines, country_code=country_code
)
lines = CheckoutLinesInfoByCheckoutTokenLoader(info.context).load(root.token)
- checkout_info = CheckoutInfoByCheckoutTokenLoader(info.context).load(root.token)
+ channel = ChannelByIdLoader(info.context).load(root.channel_id)
address = (
AddressByIdLoader(info.context).load(root.shipping_address_id)
if root.shipping_address_id
else None
)
- return Promise.all([address, lines, checkout_info]).then(
+ return Promise.all([address, lines, channel]).then(
get_available_collection_points
)