Skip to content

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

Unify rendering taxed prices

Anna Siwik9ecb10796e16
diff --git a/saleor/core/templatetags/taxed_prices.py b/saleor/core/templatetags/taxed_prices.py
index 9cffd95aa0..ea220e0991 100644
--- a/saleor/core/templatetags/taxed_prices.py
+++ b/saleor/core/templatetags/taxed_prices.py
@@ -1,4 +1,5 @@
from django import template
+from prices import MoneyRange, TaxedMoney, TaxedMoneyRange
from ...core.utils import DEFAULT_TAX_RATE_NAME
@@ -13,30 +14,6 @@ def price_range(context, price_range):
'display_gross_prices': display_gross_prices}
-def shipping_price_range(shipping_method, taxes):
- """Return shipping price to display (gross or net) depending on settings.
-
- Extra with_taxes param forces return of gross (if true)
- or net (if false) price.
- """
- return shipping_method.get_price_range(taxes)
-
-
[email protected]_tag(takes_context=True)
-def price_to_display(context, price, with_taxes=None):
- """Return price to display (gross or net) depending on settings.
-
- Extra with_taxes param forces return of gross (if true)
- or net (if false) price.
- """
- if with_taxes is None:
- display_gross_prices = context['site'].settings.display_gross_prices
- else:
- display_gross_prices = with_taxes
- return price.gross if display_gross_prices else price.net
-
-
@register.simple_tag
def tax_rate(taxes, rate_name):
"""Return tax rate value for given tax rate type in current country."""
@@ -45,3 +22,23 @@ def tax_rate(taxes, rate_name):
tax = taxes.get(rate_name) or taxes.get(DEFAULT_TAX_RATE_NAME)
return tax['value']
+
+
[email protected]_tag('taxed_price.html', takes_context=True)
+def taxed_price(context, base, display_gross=None):
+ if display_gross is None:
+ display_gross = context['site'].settings.display_gross_prices
+
+ price = base
+
+ if isinstance(base, TaxedMoneyRange):
+ if display_gross:
+ price = MoneyRange(start=base.start.gross, stop=base.stop.gross)
+ else:
+ price = MoneyRange(start=base.start.net, stop=base.stop.net)
+
+ if isinstance(base, TaxedMoney):
+ price = base.gross if display_gross else base.net
+
+ is_range = isinstance(price, MoneyRange)
+ return {'price': price, 'is_range': is_range}
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index 49d0d6dde5..3da19a1f26 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -8,7 +8,7 @@ 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 TaxedMoneyRange
+from prices import MoneyRange
from ..core.utils import format_money
from ..shipping.utils import get_taxed_shipping_price
@@ -39,12 +39,13 @@ class ShippingMethod(models.Model):
def countries(self):
return [str(country) for country in self.price_per_country.all()]
- def get_price_range(self, taxes=None):
+ @property
+ def price_range(self):
prices = [
- country.get_total_price(taxes)
+ country.get_total_price()
for country in self.price_per_country.all()]
if prices:
- return TaxedMoneyRange(min(prices), max(prices))
+ return MoneyRange(min(prices), max(prices))
return None
@@ -98,6 +99,8 @@ class ShippingMethodCountry(models.Model):
self.shipping_method, self.get_country_code_display())
def get_total_price(self, taxes=None):
+ if not taxes:
+ return self.price
return get_taxed_shipping_price(self.price, taxes)
@property
diff --git a/templates/cart/_subtotal_table.html b/templates/cart/_subtotal_table.html
index 7a3397cfce..c15251809d 100644
--- a/templates/cart/_subtotal_table.html
+++ b/templates/cart/_subtotal_table.html
@@ -1,6 +1,6 @@
{% load i18n %}
-{% load taxed_prices %}
{% load prices_i18n %}
+{% load taxed_prices %}
{% if shipping_required %}
<div class="table__row">
@@ -11,8 +11,7 @@
<div class="col-4">
<h3 class="text-right cart-subtotal" data-value="{{ cart_total.gross|amount }}" {% if local_cart_total is not None %}data-value-local="{{ local_cart_total.gross|amount }}"{% endif %}>
{% spaceless %}
- {% price_to_display cart_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price cart_total %}
<br>
<small>
({{ cart_total.net|amount:'html' }} + {{ cart_total.tax|amount:'html' }} VAT)
@@ -21,8 +20,7 @@
<br>
<small>
&asymp;
- {% price_to_display local_cart_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price local_cart_total %}
</small>
{% endif %}
{% endspaceless %}
diff --git a/templates/cart/index.html b/templates/cart/index.html
index 47b2ce0fec..6bcd50ca7a 100644
--- a/templates/cart/index.html
+++ b/templates/cart/index.html
@@ -1,9 +1,9 @@
{% extends "base.html" %}
{% load i18n %}
+{% load get_thumbnail from product_images %}cart
{% load prices_i18n %}
{% load staticfiles %}
-{% load price_to_display from taxed_prices %}
-{% load get_thumbnail from product_images %}
+{% load taxed_price from taxed_prices %}
{% block title %}{% trans "Your cart" context "Cart page title" %} — {{ block.super }}{% endblock %}
@@ -65,8 +65,7 @@
</div>
<div class="cart-item-price col-md-5 col-12" data-product-id="{{ line.variant.pk }}">
<p class="text-right">
- {% price_to_display line.get_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price line.get_total %}
</p>
<p class="small text-right">
({{ line.get_total.net|amount:'html' }} + {{ line.get_total.tax|amount:'html' }} VAT)
diff --git a/templates/cart_dropdown.html b/templates/cart_dropdown.html
index 11c4683563..8f14a413e7 100644
--- a/templates/cart_dropdown.html
+++ b/templates/cart_dropdown.html
@@ -1,9 +1,9 @@
{% load i18n %}
-{% load prices_i18n %}
-{% load staticfiles %}
{% load get_thumbnail from product_images %}
{% load placeholder %}
-{% load price_to_display from taxed_prices %}
+{% load prices_i18n %}
+{% load staticfiles %}
+{% load taxed_price from taxed_prices %}
<div class="container">
{% if quantity > 0 %}
@@ -27,8 +27,7 @@
<div class="col-md-2">
<div class="float-right">
<h3>
- {% price_to_display line.line_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price line.line_total %}
</h3>
</div>
</div>
@@ -45,8 +44,7 @@
<div class="col-md-4">
<h3 class="float-md-right price {% if lines|length <= 2 %}single{% endif %}" data-quantity="{{ quantity }}">
<p>
- {% price_to_display total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price total %}
</p>
</h3>
</div>
diff --git a/templates/checkout/details.html b/templates/checkout/details.html
index 98c28a3478..54659cb0e8 100644
--- a/templates/checkout/details.html
+++ b/templates/checkout/details.html
@@ -2,8 +2,8 @@
{% load i18n %}
{% load prices_i18n %}
-{% load price_to_display from taxed_prices %}
{% load staticfiles %}
+{% load taxed_price from taxed_prices %}
{% block title %}{% trans "Checkout" context "Checkout page title" %} — {{ block.super }}{% endblock %}
@@ -40,8 +40,7 @@
</div>
<div class="col-4">
<p class="float-right">
- {% price_to_display line_total as price %}
- {{ price|amount }}
+ {% taxed_price line_total %}
</p>
</div>
<div class="col-12">
@@ -57,12 +56,7 @@
</div>
<div class="col-4">
<p class="float-right">
- {% if checkout.shipping_method %}
- {% price_to_display checkout.get_subtotal as price %}
- {{ price|amount:'html' }}
- {% else %}
- &mdash;
- {% endif %}
+ {% taxed_price checkout.get_subtotal %}
</p>
</div>
<div class="col-12">
@@ -78,8 +72,7 @@
<div class="col-4">
<p class="float-right">
{% if checkout.shipping_method %}
- {% price_to_display checkout.shipping_price as price %}
- {{ price|amount:'html' }}
+ {% taxed_price checkout.shipping_price %}
{% else %}
&mdash;
{% endif %}
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 187a701805..be8ef85a9b 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -1,12 +1,12 @@
{% extends "dashboard/base.html" %}
{% load babel %}
-{% load prices_i18n %}
{% load i18n %}
{% load i18n_address_tags %}
-{% load taxed_prices %}
{% load materializecss %}
-{% load status %}
+{% load prices_i18n %}
{% load staticfiles %}
+{% load status %}
+{% load taxed_price from taxed_prices %}
{% load voucher %}
{% block title %}
@@ -320,8 +320,7 @@
({{ line.unit_price.net|amount:'html' }} + {{ line.unit_price.tax|amount:'html' }} VAT)
</td>
<td class="right-align">
- {% price_to_display line.unit_price order.include_taxes_in_prices as price %}
- {{ price|amount:'html' }}
+ {% taxed_price line.unit_price order.include_taxes_in_prices %}
</td>
<td class="right-align">
{% if perms.order.edit_order and order.is_draft %}
@@ -349,8 +348,7 @@
({{ line.get_total.net|amount:'html' }} + {{ line.get_total.tax|amount:'html' }} VAT)
</td>
<td class="right-align">
- {% price_to_display line.get_total order.include_taxes_in_prices as price %}
- {{ price|amount:'html' }}
+ {% taxed_price line.get_total order.include_taxes_in_prices %}
</td>
</tr>
{% endfor %}
@@ -437,15 +435,13 @@
</td>
<td class="right-align">
<p>
- {% price_to_display order.get_subtotal order.include_taxes_in_prices as price %}
- {{ price|amount:'html' }}
+ {% taxed_price order.get_subtotal order.include_taxes_in_prices %}
</p>
<p>
({{ order.get_subtotal.net|amount:'html' }} + {{ order.get_subtotal.tax|amount:'html' }} VAT)
</p>
<p>
- {% price_to_display order.shipping_price order.include_taxes_in_prices as price %}
- {{ price|amount:'html' }}
+ {% taxed_price order.shipping_price order.include_taxes_in_prices %}
</p>
<p>
({{ order.shipping_price.net|amount:'html' }} + {{ order.shipping_price.tax|amount:'html' }} VAT)
diff --git a/templates/dashboard/order/modal/cancel_line.html b/templates/dashboard/order/modal/cancel_line.html
index cf38429d82..3347594304 100644
--- a/templates/dashboard/order/modal/cancel_line.html
+++ b/templates/dashboard/order/modal/cancel_line.html
@@ -3,7 +3,7 @@
{% load i18n %}
{% load materializecss %}
{% load prices_i18n %}
-{% load price_to_display from taxed_prices %}
+{% load taxed_price from taxed_prices %}
{% block action_url %}
{% url 'dashboard:orderline-cancel' order_pk=order.pk line_pk=item.pk %}
@@ -48,15 +48,13 @@
{{ item.product_sku }}
</td>
<td class="right-align">
- {% price_to_display item.unit_price as price %}
- {{ price|amount:'html' }}
+ {% taxed_price item.unit_price order.include_taxes_in_prices %}
</td>
<td class="right-align">
{{ item.quantity }}
</td>
<td class="right-align">
- {% price_to_display item.get_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price item.get_total order.include_taxes_in_prices %}
</td>
</tr>
</tbody>
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index c2182cbbdf..2f927342dc 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -2,9 +2,9 @@
{% load i18n %}
{% load prices_i18n %}
-{% load taxed_prices %}
{% load staticfiles %}
{% load status %}
+{% load taxed_price from taxed_prices %}
{% load utils %}
{% block title %}{{ product }} - {{ block.super }}{% endblock %}
@@ -121,8 +121,7 @@
<td class="wide">{% render_variant_availability_status variant %}</td>
<td class="right-align">
{% with request.site.settings.include_taxes_in_prices as display_gross_prices %}
- {% price_to_display variant.get_price display_gross_prices as price %}
- {{ price|amount }}
+ {% taxed_price variant.get_price display_gross_prices %}
{% endwith %}
</td>
<td class="right-align">
@@ -185,14 +184,7 @@
{% endif %}
</td>
<td class="right-align">
- {% price_to_display sale_price.start display_gross_prices as start_price %}
- {% price_to_display sale_price.stop display_gross_prices as stop_price %}
-
- {% if start_price == stop_price %}
- {{ start_price|amount }}
- {% else %}
- {{ start_price|amount }} - {{ stop_price|amount }}
- {% endif %}
+ {% taxed_price sale_price display_gross_prices %}
</td>
</tr>
{% if discounted_price != sale_price %}
@@ -205,14 +197,7 @@
{% endif %}
</td>
<td class="right-align">
- {% price_to_display discounted_price.start display_gross_prices as start_price %}
- {% price_to_display discounted_price.stop display_gross_prices as stop_price %}
-
- {% if start_price == stop_price %}
- {{ start_price|amount }}
- {% else %}
- {{ start_price|amount }} - {{ stop_price|amount }}
- {% endif %}
+ {% taxed_price discounted_price display_gross_prices %}
</td>
</tr>
{% endif %}
@@ -221,14 +206,7 @@
{% trans "Purchase cost" context "Product field" %}
</td>
<td class="right-align">
- {% price_to_display purchase_cost.start display_gross_prices as start_price %}
- {% price_to_display purchase_cost.stop display_gross_prices as stop_price %}
-
- {% if start_price == stop_price %}
- {{ start_price|amount }}
- {% else %}
- {{ start_price|amount }} - {{ stop_price|amount }}
- {% endif %}
+ {% taxed_price purchase_cost display_gross_prices %}
</td>
</tr>
<tr>
diff --git a/templates/dashboard/shipping/list.html b/templates/dashboard/shipping/list.html
index f4d7fcbcff..77797df528 100644
--- a/templates/dashboard/shipping/list.html
+++ b/templates/dashboard/shipping/list.html
@@ -44,9 +44,6 @@
{% trans "Name" context "Shipping methods table header" as label %}
{% sorting_header 'name' label is_wide=True %}
- <th>
- {% trans "Price range (debug)" context "Shipping methods table header" %}
- </th>
<th>
{% trans "Price range" context "Shipping methods table header" %}
</th>
@@ -62,34 +59,10 @@
{{ method }}
</td>
<td>
- {% if not method.get_price_range %}
+ {% if not method.price_range %}
-
{% else %}
- {% with request.site.settings.include_taxes_in_prices as display_gross_prices %}
- {% shipping_price_range method request.taxes as price_range %}
-
- ({{ price_range.start.net|amount:'html' }} + {{ price_range.start.tax|amount:'html' }} VAT)
- {% if price_range.start != price_range.stop %}
- - ({{ price_range.stop.net|amount:'html' }} + {{ price_range.stop.tax|amount:'html' }} VAT)
- {% endif %}
- {% endwith %}
- {% endif %}
- </td>
- <td>
- {% if not method.get_price_range %}
- -
- {% else %}
- {% with request.site.settings.include_taxes_in_prices as display_gross_prices %}
- {% shipping_price_range method request.taxes as price_range %}
- {% price_to_display price_range.start display_gross_prices as start_price %}
- {% price_to_display price_range.stop display_gross_prices as stop_price %}
-
- {% if start_price == stop_price %}
- {{ start_price|amount }}
- {% else %}
- {{ start_price|amount }} - {{ stop_price|amount }}
- {% endif %}
- {% endwith %}
+ {% taxed_price method.price_range request.site.settings.include_taxes_in_prices %}
{% endif %}
</td>
<td>
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index 74e71aeee1..92fcf0516e 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -1,10 +1,10 @@
-{% load i18n %}
-{% load i18n_address_tags %}
{% load amount from prices_i18n %}
-{% load product_first_image from product_images %}
{% load discount_as_negative from voucher %}
-{% load taxed_prices %}
+{% load i18n %}
+{% load i18n_address_tags %}
{% load placeholder %}
+{% load product_first_image from product_images %}
+{% load taxed_price from taxed_prices %}
{% if order.user == user %}
<div class="order-details__addresses">
@@ -65,8 +65,7 @@
</div>
<div class="col-md-6 col-12">
<p class="float-right">
- {% price_to_display line.get_total as price %}
- {{ price|amount:'html' }}
+ {% taxed_price line.get_total %}
</p>
<p class="float-right">
({{ line.get_total.net|amount:'html' }} + {{ line.get_total.tax|amount:'html' }} VAT)
@@ -91,8 +90,7 @@
</div>
<div class="col-4">
<p class="float-right">
- {% price_to_display order.get_subtotal as price %}
- {{ price|amount:'html' }}
+ {% taxed_price order.get_subtotal %}
</p>
<br>
<p class="float-right">
@@ -112,8 +110,7 @@
</div>
<div class="col-4">
<p class="float-right">
- {% price_to_display order.shipping_price as price %}
- {{ price|amount:'html' }}
+ {% taxed_price order.shipping_price %}
</p>
<br>
<p class="float-right">
diff --git a/templates/product/details.html b/templates/product/details.html
index 4aa3fcac92..676da2eee7 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -1,15 +1,15 @@
{% extends "base.html" %}
{% load bootstrap_field from bootstrap4 %}
+{% load build_absolute_uri from urls %}
+{% load get_object_properties from attributes %}
+{% load get_thumbnail product_first_image from product_images %}
{% load i18n %}
{% load markdown from markdown %}
+{% load placeholder %}
{% load prices_i18n %}
-{% load taxed_prices %}
{% load staticfiles %}
-{% load get_thumbnail product_first_image from product_images %}
-{% load build_absolute_uri from urls %}
-{% load placeholder %}
-{% load get_object_properties from attributes %}
+{% load taxed_prices %}
{% block title %}
{% if product.seo_title %}
@@ -131,16 +131,14 @@
<span>{% price_range availability.price_range %}</span>
{% if availability.discount %}
<small class="product__info__price__undiscounted">
- {% price_to_display availability.price_range_undiscounted.start as price %}
- {{ price|amount:'html' }}
+ {% taxed_price availability.price_range_undiscounted %}
</small>
{% endif %}
{% if availability.price_range_local_currency %}
<br>
<small class="text-info">
&asymp;
- {% price_to_display availability.price_range_local_currency.start as price %}
- {{ price|amount:'html' }}
+ {% taxed_price availability.price_range_local_currency %}
</small>
{% endif %}
<small>
diff --git a/templates/taxed_price.html b/templates/taxed_price.html
new file mode 100644
index 0000000000..83301dc866
--- /dev/null
+++ b/templates/taxed_price.html
@@ -0,0 +1,11 @@
+{% load prices_i18n %}
+
+{% if is_range %}
+ {% if price.start == price.stop %}
+ {{ price.start|amount }}
+ {% else %}
+ {{ price.start|amount }} - {{ price.stop|amount }}
+ {% endif %}
+{% else %}
+ {{ price|amount }}
+{% endif %}