Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Display errors if no shipping methods are available
Pacu2a6b3aa765be5
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.pyindex 1cc7d1f351..c3876d5efa 100644--- a/saleor/checkout/forms.py+++ b/saleor/checkout/forms.py@@ -10,11 +10,10 @@ from django.utils.translation import npgettext_lazy, pgettext_lazyfrom django_countries.fields import LazyTypedChoiceFieldfrom ..core.exceptions import InsufficientStock-from ..core.i18n import COUNTRY_CODE_CHOICESfrom ..core.utils import format_moneyfrom ..core.utils.taxes import display_gross_prices, get_taxed_shipping_pricefrom ..discount.models import NotApplicable, Voucher-from ..shipping.models import ShippingMethod+from ..shipping.models import ShippingMethod, ShippingZonefrom ..shipping.utils import get_shipping_price_estimatefrom .models import Cart@@ -161,11 +160,17 @@ class CountryForm(forms.Form):"""Country selection form."""country = LazyTypedChoiceField(label=pgettext_lazy('Country form field label', 'Country'),- choices=COUNTRY_CODE_CHOICES)+ choices=[])def __init__(self, *args, **kwargs):self.taxes = kwargs.pop('taxes', {})super().__init__(*args, **kwargs)+ available_countries = {+ (country.code, country.name)+ for shipping_zone in ShippingZone.objects.all()+ for country in shipping_zone.countries}+ self.fields['country'].choices = sorted(+ available_countries, key=lambda choice: choice[1])def get_shipping_price_estimate(self, price, weight):"""Return a shipping price range for given order for the selecteddiff --git a/templates/checkout/_subtotal_table.html b/templates/checkout/_subtotal_table.htmlindex 04a9a4c1a8..bb1d631fe2 100644--- a/templates/checkout/_subtotal_table.html+++ b/templates/checkout/_subtotal_table.html@@ -23,31 +23,33 @@</div></div></div>- <div class="last table__row cart__delivery-info">- <div class="row">- <div class="col-7 cart__total__subtotal deliveryform" data-csrf="{{ csrf_token }}">- <div class="row">- <div class="col-12 col-md-4">- <p>{% trans "Shipping cost for:" context "Cart shipping cost form" %}</p>- </div>- <div class="col-12 col-md-8">- {{ country_form.country }}+ {% if country_form.country.field.choices %}+ <div class="last table__row cart__delivery-info">+ <div class="row">+ <div class="col-7 cart__total__subtotal deliveryform" data-csrf="{{ csrf_token }}">+ <div class="row">+ <div class="col-12 col-md-4">+ <p>{% trans "Shipping cost for:" context "Cart shipping cost form" %}</p>+ </div>+ <div class="col-12 col-md-8">+ {{ country_form.country }}+ </div></div></div>- </div>- <div class="col-5">- <div class="row">- <div class="col-md-12 col-12 text-right">- <p class="cart__delivery-info__price">- {% if shipping_price_range %}- {% price_range shipping_price_range %}- {% endif %}- </p>+ <div class="col-5">+ <div class="row">+ <div class="col-md-12 col-12 text-right">+ <p class="cart__delivery-info__price">+ {% if shipping_price_range %}+ {% price_range shipping_price_range %}+ {% endif %}+ </p>+ </div></div></div></div></div>- </div>+ {% endif %}{% endif %}<div class="cart__total"><div class="row">diff --git a/templates/checkout/shipping_method.html b/templates/checkout/shipping_method.htmlindex c2a10ad78c..bae50f6eb5 100644--- a/templates/checkout/shipping_method.html+++ b/templates/checkout/shipping_method.html@@ -8,14 +8,22 @@{% format_address cart.shipping_address %}<p><a href="{% url 'checkout:shipping-address' %}">{% trans "Select other address" %}</a></p><hr>- <form method="post" novalidate>- {% csrf_token %}- <h2>{% trans "Shipping method" context "Checkout shipping method form title" %}</h2>- {% bootstrap_form shipping_method_form show_label=False %}- <p class="text-md-right">- <button type="submit" class="btn primary">- {% trans "Continue" context "Checkout shipping method primary action" %}- </button>- </p>- </form>+ {% if shipping_method_form.shipping_method.field.choices %}+ <form method="post" novalidate>+ {% csrf_token %}+ <h2>{% trans "Shipping method" context "Checkout shipping method form title" %}</h2>+ {% bootstrap_form shipping_method_form show_label=False %}+ <p class="text-md-right">+ <button type="submit" class="btn primary">+ {% trans "Continue" context "Checkout shipping method primary action" %}+ </button>+ </p>+ </form>+ {% else %}+ {% blocktrans context "Message displayed to the user if there's no shipping methods for his country" %}+ Unfortunately we do not ship to your selected country.+ <br>+ Please enter an alternative shipping address or contact us, if you think that's a mistake.+ {% endblocktrans %}+ {% endif %}{% endblock %}diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex b371ca4794..14892991ec 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -10,7 +10,7 @@ from measurement.measures import Weightfrom prices import Money, TaxedMoneyfrom saleor.account.models import Addressfrom saleor.checkout import views-from saleor.checkout.forms import CartVoucherForm+from saleor.checkout.forms import CartVoucherForm, CountryFormfrom saleor.checkout.utils import (add_variant_to_cart, change_billing_address_in_cart,change_shipping_address_in_cart, clear_shipping_method, create_order,@@ -26,10 +26,25 @@ from saleor.discount import DiscountValueType, VoucherTypefrom saleor.discount.models import NotApplicable, Voucherfrom saleor.product.models import Categoryfrom saleor.shipping import ShippingMethodType+from saleor.shipping.models import ShippingZonefrom .utils import compare_taxes, get_redirect_location+def test_country_form_country_choices():+ form = CountryForm(data={'csrf': '', 'country': 'PL'})+ assert form.fields['country'].choices == []++ zone = ShippingZone.objects.create(countries=['PL', 'DE'], name='Europe')+ form = CountryForm(data={'csrf': '', 'country': 'PL'})++ expected_choices = [+ (country.code, country.name) for country in zone.countries]+ expected_choices = sorted(+ expected_choices, key=lambda choice: choice[1])+ assert form.fields['country'].choices == expected_choices++@pytest.mark.parametrize('minimum, maximum, value, result',(