Skip to content

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

Fix for ajax request to update checkout line items

Maciej Korycinski852216f2aa9a
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index ef248a1d8a..3ef82cc648 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -3,6 +3,8 @@ from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.response import TemplateResponse
+from saleor.core.utils.taxes import price
+
from ...account.forms import LoginForm
from ...core.utils import format_money, get_user_shipping_country, to_local_currency
from ...shipping.utils import get_shipping_price_estimate
@@ -171,7 +173,6 @@ def checkout_index(request, checkout):
"shipping_price_range": shipping_price_range,
}
)
-
return TemplateResponse(request, "checkout/index.html", context)
@@ -217,18 +218,21 @@ def update_checkout_line(request, checkout, variant_id):
)
if form.is_valid():
form.save()
+ checkout.refresh_from_db()
+ checkout_line.refresh_from_db()
+ subtotal = price(checkout_line.get_total(discounts, taxes))
response = {
"variantId": variant_id,
- "subtotal": format_money(checkout_line.get_total(discounts, taxes).gross),
+ "subtotal": format_money(subtotal),
"total": 0,
"checkout": {"numItems": checkout.quantity, "numLines": len(checkout)},
}
- checkout_total = checkout.get_subtotal(discounts, taxes)
- response["total"] = format_money(checkout_total.gross)
+ checkout_total = price(checkout.get_subtotal(discounts, taxes))
+ response["total"] = format_money(checkout_total)
local_checkout_total = to_local_currency(checkout_total, request.currency)
if local_checkout_total is not None:
- response["localTotal"] = format_money(local_checkout_total.gross)
+ response["localTotal"] = format_money(local_checkout_total)
status = 200
elif request.POST is not None:
diff --git a/saleor/core/templatetags/taxed_prices.py b/saleor/core/templatetags/taxed_prices.py
index 4ef49ca8d1..442eb796e1 100644
--- a/saleor/core/templatetags/taxed_prices.py
+++ b/saleor/core/templatetags/taxed_prices.py
@@ -1,7 +1,7 @@
from django import template
from prices import MoneyRange, TaxedMoney, TaxedMoneyRange
-from ...core.utils.taxes import DEFAULT_TAX_RATE_NAME
+from ...core.utils.taxes import DEFAULT_TAX_RATE_NAME, price as get_price
register = template.Library()
@@ -28,14 +28,7 @@ def price(context, base, display_gross=None, html=True):
if display_gross is None:
display_gross = context["site"].settings.display_gross_prices
- if isinstance(base, TaxedMoneyRange):
- if display_gross:
- base = MoneyRange(start=base.start.gross, stop=base.stop.gross)
- else:
- base = MoneyRange(start=base.start.net, stop=base.stop.net)
-
- if isinstance(base, TaxedMoney):
- base = base.gross if display_gross else base.net
+ base = get_price(base, display_gross)
is_range = isinstance(base, MoneyRange)
return {"price": base, "is_range": is_range, "html": html}
diff --git a/saleor/core/utils/taxes.py b/saleor/core/utils/taxes.py
index b4499cc56e..dbf72012ce 100644
--- a/saleor/core/utils/taxes.py
+++ b/saleor/core/utils/taxes.py
@@ -1,3 +1,5 @@
+from typing import Union
+
from django.conf import settings
from django.contrib.sites.models import Site
from django_countries.fields import Country
@@ -104,3 +106,18 @@ def get_taxed_shipping_price(shipping_price, taxes):
if not charge_taxes_on_shipping():
taxes = None
return apply_tax_to_price(taxes, DEFAULT_TAX_RATE_NAME, shipping_price)
+
+
+def price(base: Union[TaxedMoney, TaxedMoneyRange], display_gross=None) -> Money:
+ """Return price amount that should be displayed based on settings"""
+ if not display_gross:
+ display_gross = display_gross_prices()
+ if isinstance(base, TaxedMoneyRange):
+ if display_gross:
+ base = MoneyRange(start=base.start.gross, stop=base.stop.gross)
+ else:
+ base = MoneyRange(start=base.start.net, stop=base.stop.net)
+
+ if isinstance(base, TaxedMoney):
+ base = base.gross if display_gross else base.net
+ return base