Skip to content

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

Improve discounts api, fix decimal empty string

Michał Jelonek3279a77ee28a
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index a649c15127..b45ad30d43 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -16,7 +16,6 @@ from ..account.forms import get_address_form
from ..account.models import Address
from ..account.utils import store_user_address
from ..core.exceptions import InsufficientStock
-from ..core.i18n import ANY_COUNTRY
from ..core.utils import to_local_currency
from ..core.utils.taxes import ZERO_MONEY, get_taxes_for_country
from ..discount import VoucherType
@@ -652,14 +651,15 @@ 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 = all([
- voucher.countries, ANY_COUNTRY not in voucher.countries,
- cart.shipping_address.country.code not in voucher.countries])
- if not_valid_for_country:
+
+ # check if voucher is limited to specified countries
+ shipping_country = cart.shipping_address.country
+ if voucher.countries and shipping_country.code not in voucher.countries:
msg = pgettext(
'Voucher not applicable',
'This offer is not valid in your country.')
raise NotApplicable(msg)
+
return get_shipping_voucher_discount(
voucher, cart.get_subtotal(), shipping_method.get_total())
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 04173681dc..240368746f 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -4,10 +4,10 @@ from django import forms
from django.conf import settings
from django.urls import reverse_lazy
from django.utils.translation import pgettext_lazy
+from django_countries import countries
from django_prices.forms import MoneyField
from mptt.forms import TreeNodeMultipleChoiceField
-from ...core.i18n import COUNTRY_CODE_CHOICES
from ...core.utils.taxes import ZERO_MONEY
from ...discount import DiscountValueType
from ...discount.models import Sale, Voucher
@@ -130,7 +130,7 @@ class VoucherForm(forms.ModelForm):
class ShippingVoucherForm(forms.ModelForm):
min_amount_spent = MinAmountSpent
countries = forms.MultipleChoiceField(
- choices=COUNTRY_CODE_CHOICES,
+ choices=countries,
required=False,
label=pgettext_lazy(
'Text above the dropdown of countries',
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 2c474e1692..44ed7e0baf 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -216,9 +216,9 @@ class SaleInput(graphene.InputObjectType):
collections = graphene.List(
graphene.ID, description='Collections related to the discount.',
name='collections')
- start_date = graphene.types.datetime.Date(
+ start_date = graphene.Date(
description='Start date of the sale in ISO 8601 format.')
- end_date = graphene.types.datetime.Date(
+ end_date = graphene.Date(
description='End date of the sale in ISO 8601 format.')
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 58725ba288..e2408ebef6 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -600,6 +600,41 @@ def test_get_discount_for_cart_shipping_voucher(
assert discount == Money(expected_value, 'USD')
+def test_get_discount_for_cart_shipping_voucher_all_countries():
+ subtotal = TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD'))
+ shipping_total = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
+ cart = Mock(
+ get_subtotal=Mock(return_value=subtotal),
+ is_shipping_required=Mock(return_value=True),
+ shipping_method=Mock(get_total=Mock(return_value=shipping_total)),
+ shipping_address=Mock(country=Country('PL')))
+ voucher = Voucher(
+ code='unique', type=VoucherType.SHIPPING,
+ discount_value_type=DiscountValueType.PERCENTAGE,
+ discount_value=50, countries=[])
+
+ discount = get_voucher_discount_for_cart(voucher, cart)
+
+ assert discount == Money(5, 'USD')
+
+
+def test_get_discount_for_cart_shipping_voucher_limited_countries():
+ subtotal = TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD'))
+ shipping_total = TaxedMoney(net=Money(10, 'USD'), gross=Money(10, 'USD'))
+ cart = Mock(
+ get_subtotal=Mock(return_value=subtotal),
+ is_shipping_required=Mock(return_value=True),
+ shipping_method=Mock(get_total=Mock(return_value=shipping_total)),
+ shipping_address=Mock(country=Country('PL')))
+ voucher = Voucher(
+ code='unique', type=VoucherType.SHIPPING,
+ discount_value_type=DiscountValueType.PERCENTAGE,
+ discount_value=50, countries=['UK', 'DE'])
+
+ with pytest.raises(NotApplicable):
+ get_voucher_discount_for_cart(voucher, cart)
+
+
@pytest.mark.parametrize(
'is_shipping_required, shipping_method, discount_value, discount_type,'
'countries, min_amount_spent, subtotal, error_msg', [