Skip to content

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

Validate shipping method due to shipping address in checkout

Anna Siwik8576b9a638e4
diff --git a/saleor/cart/checkout/utils.py b/saleor/cart/checkout/utils.py
index 81c6c23ca8..e230f88619 100644
--- a/saleor/cart/checkout/utils.py
+++ b/saleor/cart/checkout/utils.py
@@ -115,7 +115,7 @@ def get_billing_forms_with_shipping(cart, data, user_addresses, country):
autocomplete_type='billing',
initial={'country': billing_address.country})
addresses_form = BillingAddressChoiceForm(
- data, additional_addresses=user_addresses, initial={
+ data, addresses=user_addresses, initial={
'address': billing_address.id})
else:
address_form, preview = get_address_form(
diff --git a/saleor/cart/checkout/views/validators.py b/saleor/cart/checkout/views/validators.py
index 647188045c..6be460de00 100644
--- a/saleor/cart/checkout/views/validators.py
+++ b/saleor/cart/checkout/views/validators.py
@@ -3,6 +3,8 @@ from functools import wraps
from django.core.exceptions import ValidationError
from django.shortcuts import redirect
+from ....shipping.models import ShippingMethodCountry
+
def validate_cart(view):
"""Decorate a view making it require a non-empty cart.
@@ -40,7 +42,8 @@ def validate_shipping_address(view):
def validate_shipping_method(view):
"""Decorate a view making it require a shipping method.
- If the method is missing, redirect to the shipping method step.
+ If the method is missing or incorrect, redirect to the shipping method
+ step.
Expects to be decorated with `@validate_cart`.
"""
@@ -48,6 +51,13 @@ def validate_shipping_method(view):
def func(request, cart):
if cart.shipping_method is None:
return redirect('cart:checkout-shipping-method')
+ country_code = cart.shipping_address.country.code
+ valid_methods = ShippingMethodCountry.objects.select_related(
+ 'shipping_method').unique_for_country_code(country_code)
+ if cart.shipping_method not in valid_methods:
+ cart.shipping_method = None
+ cart.save()
+ return redirect('cart:checkout-shipping-method')
return view(request, cart)
return func