Skip to content

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

Handle cart taxes conditionally

Anna Siwik6a84026ac2a5
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index bd4a447488..c5b3ebcd08 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -3,6 +3,7 @@ from datetime import date, timedelta
from functools import wraps
from uuid import UUID
+from django.conf import settings
from django.contrib import messages
from django.db import transaction
from django.db.models import Sum
@@ -181,17 +182,14 @@ def get_cart_from_request(request, cart_queryset=Cart.objects.all()):
if cart is not None:
return cart
if user:
- return Cart(
- user=user,
- shipping_address=user.default_shipping_address,
- billing_address=user.default_billing_address)
+ return Cart(user=user)
return Cart()
def get_or_create_db_cart(cart_queryset=Cart.objects.all()):
"""Decorate view to always receive a saved cart instance.
- Changes the view signature from `fund(request, ...)` to
+ Changes the view signature from `func(request, ...)` to
`func(request, cart, ...)`.
If no matching cart is found, one will be created and a cookie will be set
@@ -213,7 +211,7 @@ def get_or_create_db_cart(cart_queryset=Cart.objects.all()):
def get_or_empty_db_cart(cart_queryset=Cart.objects.all()):
"""Decorate view to receive a cart if one exists.
- Changes the view signature from `fund(request, ...)` to
+ Changes the view signature from `func(request, ...)` to
`func(request, cart, ...)`.
If no matching cart is found, an unsaved `Cart` instance will be used.
@@ -709,7 +707,10 @@ def remove_voucher_from_cart(cart):
def get_taxes_for_cart(cart, default_taxes):
- """Return taxes based on shipping address (if set) or default one."""
+ """Return taxes (if handled) due to shipping address or default one."""
+ if not settings.VATLAYER_ACCESS_KEY:
+ return None
+
if cart.shipping_address:
return get_taxes_for_country(cart.shipping_address.country)
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index e83464d207..ce6e0be590 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -593,6 +593,16 @@ def test_get_taxes_for_cart_with_shipping_address(cart, address, vatlayer):
compare_taxes(taxes, get_taxes_for_country(Country('DE')))
+def test_get_taxes_for_cart_with_shipping_address_taxes_not_handled(
+ cart, settings, address, vatlayer):
+ settings.VATLAYER_ACCESS_KEY = ''
+ address.country = 'DE'
+ address.save()
+ cart.shipping_address = address
+ cart.save()
+ assert not get_taxes_for_cart(cart, None)
+
+
def test_get_voucher_for_cart(cart_with_voucher, voucher):
cart_voucher = get_voucher_for_cart(cart_with_voucher)
assert cart_voucher == voucher