Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Validate and clean shipping method in cart
Anna Siwik16394861e62c
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex c5b3ebcd08..689d4755f4 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -24,6 +24,7 @@ from ..discount.utils import (get_product_or_category_voucher_discount, get_shipping_voucher_discount,get_value_voucher_discount, increase_voucher_usage)from ..order.models import Order+from ..shipping.models import ShippingMethodCountryfrom .forms import (AddressChoiceForm, AnonymousUserBillingForm, AnonymousUserShippingForm,BillingAddressChoiceForm)@@ -272,7 +273,8 @@ def change_cart_user(cart, user):open_cart.delete()cart.user = usercart.shipping_address = user.default_shipping_address- cart.save(update_fields=['user', 'shipping_address'])+ cart.billing_address = user.default_billing_address+ cart.save(update_fields=['user', 'shipping_address', 'billing_address'])def update_cart_quantity(cart):@@ -717,6 +719,18 @@ def get_taxes_for_cart(cart, default_taxes):return default_taxes+def check_shipping_method(cart):+ """Check if shipping method is valid for address and remove (if not)."""+ 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 False+ return True++@transaction.atomicdef create_order(cart, tracking_code, discounts, taxes):"""Create an order from the cart.diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.pyindex 9f70440ad0..f1d6d5c5ce 100644--- a/saleor/checkout/views/__init__.py+++ b/saleor/checkout/views/__init__.py@@ -11,7 +11,7 @@ from ...shipping.utils import get_shipment_optionsfrom ..forms import CartShippingMethodForm, CountryForm, ReplaceCartLineFormfrom ..models import Cartfrom ..utils import (- check_product_availability_and_warn, get_cart_data,+ check_product_availability_and_warn, check_shipping_method, get_cart_data,get_cart_data_for_checkout, get_or_empty_db_cart, get_taxes_for_cart)from .discount import add_voucher_form, validate_voucherfrom .shipping import (@@ -63,6 +63,7 @@ def checkout_shipping_address(request, cart):def checkout_shipping_method(request, cart):"""Display the shipping method selection step."""taxes = get_taxes_for_cart(cart, request.taxes)+ check_shipping_method(cart)form = CartShippingMethodForm(request.POST or None, taxes=taxes, instance=cart,initial={'shipping_method': cart.shipping_method})diff --git a/saleor/checkout/views/validators.py b/saleor/checkout/views/validators.pyindex 11fdccd5d3..40cd227624 100644--- a/saleor/checkout/views/validators.py+++ b/saleor/checkout/views/validators.py@@ -3,7 +3,7 @@ from functools import wrapsfrom django.core.exceptions import ValidationErrorfrom django.shortcuts import redirect-from ...shipping.models import ShippingMethodCountry+from ..utils import check_shipping_methoddef validate_cart(view):@@ -49,14 +49,7 @@ def validate_shipping_method(view):"""@wraps(view)def func(request, cart):- if cart.shipping_method is None:- return redirect('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()+ if not cart.shipping_method or not check_shipping_method(cart):return redirect('checkout:shipping-method')return view(request, cart)return func