Skip to content

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

Change shipping discount voucher choices

Pacu2adb7cc3f3302
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index ad5afb35d0..bfc7813b25 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -15,6 +15,7 @@ from . import AddressType, logger
from ..account.forms import get_address_form
from ..account.models import Address
from ..account.utils import store_user_address
+from ..core.i18n import ANY_COUNTRY
from ..core.exceptions import InsufficientStock
from ..core.utils import to_local_currency
from ..core.utils.taxes import ZERO_MONEY, get_taxes_for_country
@@ -651,9 +652,9 @@ def _get_shipping_voucher_discount_for_cart(voucher, cart):
'Voucher not applicable',
'Please select a shipping method first.')
raise NotApplicable(msg)
- not_valid_for_country = (
- voucher.countries and
- shipping_method.country_code not in voucher.countries)
+ not_valid_for_country = all([
+ voucher.countries, ANY_COUNTRY not in voucher.countries,
+ shipping_method.country_code not in voucher.countries])
if not_valid_for_country:
msg = pgettext(
'Voucher not applicable',
diff --git a/saleor/core/i18n.py b/saleor/core/i18n.py
index d78786e9de..045359d95b 100644
--- a/saleor/core/i18n.py
+++ b/saleor/core/i18n.py
@@ -2,7 +2,7 @@ from django.utils.translation import pgettext_lazy
from django_countries import countries
ANY_COUNTRY = ''
-ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Rest of World')
+ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Any Country')
COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, ANY_COUNTRY_DISPLAY)] + list(countries)
VAT_RATE_TYPE_TRANSLATIONS = {
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index f8935485be..a505a4bb0b 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -7,7 +7,7 @@ from django.utils.translation import pgettext_lazy
from django_prices.forms import MoneyField
from mptt.forms import TreeNodeMultipleChoiceField
-from ...core.i18n import COUNTRY_CODE_CHOICES
+from ...core.i18n import COUNTRY_CODE_CHOICES, ANY_COUNTRY
from ...core.utils.taxes import ZERO_MONEY
from ...discount import DiscountValueType
from ...discount.models import Sale, Voucher
@@ -131,11 +131,14 @@ class VoucherForm(forms.ModelForm):
def country_choices():
country_codes = ShippingMethodCountry.objects.all()
country_codes = country_codes.values_list('country_code', flat=True)
- country_codes = country_codes.distinct()
+ country_codes = list(country_codes.distinct())
+ if ANY_COUNTRY in country_codes:
+ return COUNTRY_CODE_CHOICES
country_dict = dict(COUNTRY_CODE_CHOICES)
- return [
+ country_choices = [
(country_code, country_dict[country_code])
for country_code in country_codes]
+ return country_choices
class ShippingVoucherForm(forms.ModelForm):
diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.py
index a2d2f4f26e..51c20df99e 100644
--- a/tests/dashboard/test_discounts.py
+++ b/tests/dashboard/test_discounts.py
@@ -7,10 +7,14 @@ import pytest
from django.urls import reverse
from prices import Money, TaxedMoney
+from saleor.core.i18n import ANY_COUNTRY, COUNTRY_CODE_CHOICES
+from saleor.core.utils import get_country_name_by_code
+from saleor.dashboard.discount.forms import country_choices
from saleor.dashboard.order.utils import get_voucher_discount_for_order
from saleor.discount import DiscountValueType, VoucherType
-from saleor.discount.models import Sale, Voucher, NotApplicable
+from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.product.models import Collection
+from saleor.shipping.models import ShippingMethodCountry
def test_sales_list(admin_client, sale):
@@ -191,3 +195,15 @@ def test_ajax_voucher_list(admin_client, voucher):
assert response.status_code == 200
assert resp_decoded == {'results': vouchers_list}
+
+
+def test_country_choices(shipping_method):
+ any_country_shipping_method = ShippingMethodCountry.objects.filter(
+ country_code=ANY_COUNTRY)
+ assert any_country_shipping_method.exists()
+ assert country_choices() == COUNTRY_CODE_CHOICES
+
+ any_country_shipping_method.delete()
+ ShippingMethodCountry.objects.create(
+ country_code='PL', price=10, shipping_method=shipping_method)
+ assert country_choices() == [('PL', get_country_name_by_code('PL'))]