Skip to content

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

Ability to configure shipping rates

Anna Siwik187521f70f64
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index e4260bf01f..06e9ad7b5e 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -46,7 +46,8 @@ def index(request, cart):
default_country = get_user_shipping_country(request)
country_form = CountryForm(initial={'country': default_country})
- default_country_options = get_shipment_options(default_country)
+ default_country_options = get_shipment_options(
+ default_country, request.taxes)
cart_data = get_cart_data(
cart, default_country_options, request.currency, discounts, taxes)
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 3d91de977b..0bb37a786b 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -15,6 +15,7 @@ from ..account.utils import store_user_address
from ..cart.models import Cart
from ..cart.utils import get_or_empty_db_cart
from ..core import analytics
+from ..core.utils import ZERO_TAXED_MONEY
from ..discount.models import NotApplicable, Voucher
from ..discount.utils import increase_voucher_usage
from ..order.models import Order
@@ -147,6 +148,13 @@ class Checkout:
self.modified = True
self._shipping_method = shipping_method_country
+ @property
+ def shipping_price(self):
+ shipping_method = self.shipping_method
+ return (
+ shipping_method.get_total_price(self.taxes) if shipping_method
+ else ZERO_TAXED_MONEY)
+
@property
def email(self):
"""Return the customer email if any."""
@@ -291,13 +299,6 @@ class Checkout:
self._add_to_user_address_book(
self.billing_address, is_billing=True)
- if self.shipping_method:
- shipping_price = self.shipping_method.get_total_price()
- else:
- shipping_price = TaxedMoney(
- net=Money(0, settings.DEFAULT_CURRENCY),
- gross=Money(0, settings.DEFAULT_CURRENCY))
-
shipping_method_name = (
smart_text(self.shipping_method) if self.is_shipping_required
else None)
@@ -308,7 +309,7 @@ class Checkout:
'billing_address': billing_address,
'shipping_address': shipping_address,
'tracking_client_id': self.tracking_code,
- 'shipping_price': shipping_price,
+ 'shipping_price': self.shipping_price,
'shipping_method_name': shipping_method_name,
'total': self.get_total(),
'include_taxes_in_prices': include_taxes_in_prices}
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.py
index d766de318c..5cdf802b98 100644
--- a/saleor/checkout/forms.py
+++ b/saleor/checkout/forms.py
@@ -1,10 +1,12 @@
"""Checkout-related forms."""
from django import forms
+from django.contrib.sites.models import Site
from django.utils.safestring import mark_safe
from django.utils.translation import pgettext_lazy
from ..core.utils import format_money
from ..shipping.models import ShippingMethodCountry
+from ..shipping.utils import get_taxed_shipping_price
class CheckoutAddressField(forms.ChoiceField):
@@ -66,12 +68,24 @@ class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect()
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.taxes = None
+
def label_from_instance(self, obj):
"""Return a friendly label for the shipping method."""
- price_html = format_money(obj.price)
+ price = get_taxed_shipping_price(obj.price, self.taxes)
+ if Site.objects.get_current().settings.display_gross_prices:
+ price = price.gross
+ else:
+ price = price.net
+ price_html = format_money(price)
label = mark_safe('%s %s' % (obj.shipping_method, price_html))
return label
+ def set_taxes(self, taxes):
+ self.taxes = taxes
+
class ShippingMethodForm(forms.Form):
"""Shipping method form."""
@@ -83,9 +97,10 @@ class ShippingMethodForm(forms.Form):
'Shipping method form field label', 'Shipping method'),
required=True)
- def __init__(self, country_code, *args, **kwargs):
+ def __init__(self, country_code, taxes, *args, **kwargs):
super().__init__(*args, **kwargs)
method_field = self.fields['method']
+ method_field.set_taxes(taxes)
if country_code:
queryset = method_field.queryset
method_field.queryset = queryset.unique_for_country_code(
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index cc376a7849..469550c315 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -45,7 +45,7 @@ def shipping_method_view(request, checkout):
"""Display the shipping method selection step."""
country_code = checkout.shipping_address.country.code
shipping_method_form = ShippingMethodForm(
- country_code, request.POST or None,
+ country_code, request.taxes, request.POST or None,
initial={'method': checkout.shipping_method})
if shipping_method_form.is_valid():
checkout.shipping_method = shipping_method_form.cleaned_data['method']
diff --git a/saleor/dashboard/sites/forms.py b/saleor/dashboard/sites/forms.py
index b2603a517c..bba114e2a8 100644
--- a/saleor/dashboard/sites/forms.py
+++ b/saleor/dashboard/sites/forms.py
@@ -30,14 +30,19 @@ class SiteSettingsForm(forms.ModelForm):
class SiteSettingsTaxesForm(forms.ModelForm):
class Meta:
model = SiteSettings
- fields = ['include_taxes_in_prices', 'display_gross_prices']
+ fields = (
+ 'include_taxes_in_prices', 'display_gross_prices',
+ 'charge_taxes_on_shipping')
labels = {
'include_taxes_in_prices': pgettext_lazy(
'Include taxes in prices',
'All taxes are included in my prices'),
'display_gross_prices': pgettext_lazy(
'Display gross prices',
- 'Show customers gross prices in storefront')}
+ 'Show customers gross prices in storefront'),
+ 'charge_taxes_on_shipping': pgettext_lazy(
+ 'Charge taxes on shipping rates',
+ 'Charge taxes on shipping rates')}
class AuthorizationKeyForm(forms.ModelForm):
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index 01596564e7..434b2a29bd 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -8,9 +8,10 @@ from django.utils.safestring import mark_safe
from django.utils.translation import pgettext_lazy
from django_countries import countries
from django_prices.models import MoneyField
-from prices import TaxedMoney, TaxedMoneyRange
+from prices import TaxedMoneyRange
from ..core.utils import format_money
+from ..shipping.utils import get_taxed_shipping_price
ANY_COUNTRY = ''
ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Rest of World')
@@ -97,8 +98,8 @@ class ShippingMethodCountry(models.Model):
return '%s %s' % (
self.shipping_method, self.get_country_code_display())
- def get_total_price(self):
- return TaxedMoney(net=self.price, gross=self.price)
+ def get_total_price(self, taxes=None):
+ return get_taxed_shipping_price(self.price, taxes)
@property
def ajax_label(self):
diff --git a/saleor/shipping/utils.py b/saleor/shipping/utils.py
index b5b40ff48a..8cbb4d38bc 100644
--- a/saleor/shipping/utils.py
+++ b/saleor/shipping/utils.py
@@ -1,9 +1,20 @@
-from prices import TaxedMoney, TaxedMoneyRange
+from django.contrib.sites.models import Site
+from prices import MoneyRange
-from .models import ShippingMethodCountry
+from ..core.utils import apply_tax_to_price
-def get_shipment_options(country_code):
+def get_taxed_shipping_price(price, taxes=None):
+ """Calculate shipping price based on settings and taxes."""
+ charge_taxes = (
+ Site.objects.get_current().settings.charge_taxes_on_shipping)
+ if not charge_taxes:
+ taxes = None
+ return apply_tax_to_price(taxes, 'standard', price)
+
+
+def get_shipment_options(country_code, taxes=None):
+ from .models import ShippingMethodCountry
shipping_methods_qs = ShippingMethodCountry.objects.select_related(
'shipping_method')
shipping_methods = shipping_methods_qs.filter(country_code=country_code)
@@ -11,8 +22,6 @@ def get_shipment_options(country_code):
shipping_methods = shipping_methods_qs.filter(country_code='')
if shipping_methods:
shipping_methods = shipping_methods.values_list('price', flat=True)
- min_amount = min(shipping_methods)
- max_amount = max(shipping_methods)
- return TaxedMoneyRange(
- start=TaxedMoney(net=min_amount, gross=min_amount),
- stop=TaxedMoney(net=max_amount, gross=max_amount))
+ prices = MoneyRange(
+ start=min(shipping_methods), stop=max(shipping_methods))
+ return get_taxed_shipping_price(prices, taxes)
diff --git a/saleor/site/migrations/0014_handle_taxes.py b/saleor/site/migrations/0014_handle_taxes.py
index c091fb7907..2f172ad0e3 100644
--- a/saleor/site/migrations/0014_handle_taxes.py
+++ b/saleor/site/migrations/0014_handle_taxes.py
@@ -20,4 +20,9 @@ class Migration(migrations.Migration):
name='include_taxes_in_prices',
field=models.BooleanField(default=True),
),
+ migrations.AddField(
+ model_name='sitesettings',
+ name='charge_taxes_on_shipping',
+ field=models.BooleanField(default=True),
+ ),
]
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 4b62e8fec0..fc39ca70e9 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -21,6 +21,7 @@ class SiteSettings(models.Model):
null=True)
include_taxes_in_prices = models.BooleanField(default=True)
display_gross_prices = models.BooleanField(default=True)
+ charge_taxes_on_shipping = models.BooleanField(default=True)
class Meta:
permissions = (
diff --git a/templates/checkout/details.html b/templates/checkout/details.html
index c81be9b5b6..e8dd812f57 100644
--- a/templates/checkout/details.html
+++ b/templates/checkout/details.html
@@ -62,7 +62,7 @@
<div class="col-4">
<p class="float-right">
{% if checkout.shipping_method %}
- {% price_to_display checkout.shipping_method.get_total_price as price %}
+ {% price_to_display checkout.shipping_price as price %}
{{ price|amount:'html' }}
{% else %}
&mdash;
diff --git a/templates/dashboard/sites/detail.html b/templates/dashboard/sites/detail.html
index 0a6703c435..f7bb0d5f9d 100644
--- a/templates/dashboard/sites/detail.html
+++ b/templates/dashboard/sites/detail.html
@@ -100,6 +100,20 @@
</p>
</div>
</div>
+ <div class="row">
+ <div class="col s12 m4 l12">
+ <h4>
+ {% trans "Charge taxes on shipping rates" context "Site settings field" %}
+ </h4>
+ <p>
+ {% if site.charge_taxes_on_shipping %}
+ {% trans "Yes" context "Charge taxes on shipping rates setting description" %}
+ {% else %}
+ {% trans "No" context "Charge taxes on shipping rates setting description" %}
+ {% endif %}
+ </p>
+ </div>
+ </div>
</div>
<div class="card-action">
<a class="btn-flat waves-effect" href="{% url 'dashboard:site-configure-taxes' pk=site.pk %}">
diff --git a/templates/dashboard/sites/taxes_form.html b/templates/dashboard/sites/taxes_form.html
index bcbc75b5dd..93a39da24f 100644
--- a/templates/dashboard/sites/taxes_form.html
+++ b/templates/dashboard/sites/taxes_form.html
@@ -1,4 +1,5 @@
{% extends 'dashboard/base.html' %}
+{% load staticfiles %}
{% load i18n %}
{% load materializecss %}
@@ -47,7 +48,7 @@
<div class="col s12 checkbox-helptext">
<h6>
{% blocktrans context "Dashboard site taxes settings form help text" %}
- If selected, all prices entered in dashboard will include taxes.
+ If selected, all prices entered and displayed in dashboard will include taxes.
{% endblocktrans %}
</h6>
<h6>
@@ -69,6 +70,18 @@
</h6>
</div>
</div>
+ <div class="row">
+ <div class="col s12">
+ {{ taxes_form.charge_taxes_on_shipping|materializecss }}
+ </div>
+ <div class="col s12 checkbox-helptext">
+ <h6>
+ {% blocktrans context "Dashboard site taxes settings form help text" %}
+ If selected, standard rate tax is charged on shipping price.
+ {% endblocktrans %}
+ </h6>
+ </div>
+ </div>
</div>
<div class="card-action right-align">
<a class="btn-flat waves-effect" href="{% url 'dashboard:site-details' pk=site.pk %}" target="_blank">
@@ -81,6 +94,13 @@
</form>
</div>
</div>
- <div class="col m12 l3 help-box"></div>
+ <div class="col m12 l3 help-box">
+ <p class="grey-text">
+ <svg data-src="{% static "dashboard/images/help.svg" %}" width="18" height="18" />
+ {% blocktrans trimmed context "Taxes help text" %}
+ Taxes are charged according to the standard rate applicable in the country to which the order is delivered. For now, only rates in European Union are handled.
+ {% endblocktrans %}
+ </p>
+ </div>
</div>
{% endblock %}
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index 18f3c87e70..f678ad2556 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -3,6 +3,7 @@
{% load amount from prices_i18n %}
{% load product_first_image from product_images %}
{% load discount_as_negative from voucher %}
+{% load price_ranges %}
{% load placeholder %}
{% if order.user == user %}
@@ -64,7 +65,7 @@
</div>
<div class="col-md-6 col-12">
<p class="float-right">
- {% price_to_display item.get_total as price %}
+ {% price_to_display line.get_total as price %}
{{ price|amount:'html' }}
</p>
</div>
diff --git a/tests/test_cart.py b/tests/test_cart.py
index eda25420cd..f6106f3da7 100644
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@ -589,8 +589,7 @@ def test_cart_summary_page(client, product, request_cart):
content = response.context
assert content['quantity'] == request_cart.quantity
cart_total = request_cart.get_total()
- assert content['total'] == currencyfmt(
- cart_total.gross.amount, cart_total.currency)
+ assert content['total'] == cart_total
assert len(content['lines']) == 1
cart_line = content['lines'][0]
assert cart_line['variant'] == variant.name