Skip to content

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

Print vat percente for vatlayer on storefront

Maciej Korycinski7334a61b1cda
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.py
index bcc5aa71f6..34bf737309 100644
--- a/saleor/checkout/forms.py
+++ b/saleor/checkout/forms.py
@@ -8,7 +8,7 @@ from django.core.exceptions import NON_FIELD_ERRORS, ObjectDoesNotExist
from django.utils.encoding import smart_text
from django.utils.safestring import mark_safe
from django.utils.translation import npgettext_lazy, pgettext_lazy
-from django_countries.fields import LazyTypedChoiceField
+from django_countries.fields import Country, LazyTypedChoiceField
from ..core.exceptions import InsufficientStock
from ..core.taxes import display_gross_prices
@@ -205,6 +205,8 @@ class CountryForm(forms.Form):
country.
"""
country = self.cleaned_data["country"]
+ if isinstance(country, str):
+ country = Country(country)
return get_shipping_price_estimate(price, weight, country)
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index b26641f911..8c12d1096e 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -17,12 +17,12 @@ from ..account.utils import store_user_address
from ..core.exceptions import InsufficientStock
from ..core.taxes import ZERO_MONEY
from ..core.taxes.interface import (
+ checkout_are_taxes_handled,
get_line_total_gross,
get_shipping_gross,
get_subtotal_gross,
get_total_gross,
postprocess_order_creation,
- show_taxes_on_storefront,
)
from ..core.utils import to_local_currency
from ..discount import VoucherType
@@ -655,7 +655,7 @@ def get_checkout_context(checkout, discounts, currency=None, shipping_range=None
context = {
"checkout": checkout,
- "checkout_are_taxes_handled": show_taxes_on_storefront(),
+ "checkout_are_taxes_handled": checkout_are_taxes_handled(),
"checkout_lines": [
(line, get_line_total_gross(line, discounts)) for line in checkout
],
diff --git a/saleor/core/taxes/avatax/__init__.py b/saleor/core/taxes/avatax/__init__.py
index c48c6b30b9..3b71b90a86 100644
--- a/saleor/core/taxes/avatax/__init__.py
+++ b/saleor/core/taxes/avatax/__init__.py
@@ -82,6 +82,8 @@ def validate_checkout(checkout: "Checkout") -> bool:
return False
if not shipping_address:
return False
+ if not checkout.shipping_method:
+ return False
return True
diff --git a/saleor/core/taxes/interface.py b/saleor/core/taxes/interface.py
index f202a52dc0..166b535527 100644
--- a/saleor/core/taxes/interface.py
+++ b/saleor/core/taxes/interface.py
@@ -134,6 +134,14 @@ def show_taxes_on_storefront() -> bool:
return False
+def checkout_are_taxes_handled() -> bool:
+ if settings.VATLAYER_ACCESS_KEY:
+ return True
+ if settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:
+ return True
+ return False
+
+
def apply_taxes_to_shipping_price_range(prices: MoneyRange, country: Country):
if country:
if settings.VATLAYER_ACCESS_KEY:
diff --git a/saleor/core/taxes/vatlayer/interface.py b/saleor/core/taxes/vatlayer/interface.py
index be56717ced..fede8a45a6 100644
--- a/saleor/core/taxes/vatlayer/interface.py
+++ b/saleor/core/taxes/vatlayer/interface.py
@@ -47,6 +47,8 @@ def get_shipping_gross(checkout: "Checkout", _: "SaleQueryset") -> TaxedMoney:
"""Calculate shipping gross for checkout"""
address = checkout.shipping_address or checkout.billing_address
taxes = get_taxes_for_address(address)
+ if not checkout.shipping_method:
+ return ZERO_TAXED_MONEY
return get_taxed_shipping_price(checkout.shipping_method.price, taxes)
diff --git a/saleor/product/utils/variants_picker.py b/saleor/product/utils/variants_picker.py
index 1f49e0fd53..15844cad7e 100644
--- a/saleor/product/utils/variants_picker.py
+++ b/saleor/product/utils/variants_picker.py
@@ -4,12 +4,15 @@ from django_prices.templatetags import prices_i18n
from ...core.taxes import display_gross_prices
from ...core.taxes.interface import apply_taxes_to_variant, show_taxes_on_storefront
+from ...core.taxes.vatlayer import get_tax_rate_by_name
from ...core.utils import to_local_currency
from ...seo.schema.product import variant_json_ld
from .availability import get_product_availability
-def get_variant_picker_data(product, discounts=None, country=None, local_currency=None):
+def get_variant_picker_data(
+ product, discounts=None, taxes=None, local_currency=None, country=None
+):
availability = get_product_availability(product, discounts, country, local_currency)
variants = product.variants.all()
data = {"variantAttributes": [], "variants": []}
@@ -68,7 +71,8 @@ def get_variant_picker_data(product, discounts=None, country=None, local_currenc
data["availability"] = {
"discount": price_as_dict(availability.discount),
- "taxRate": 0,
+ # FIXME this should use plugin hooks after we will introduce plugin architecure.
+ "taxRate": get_tax_rate_by_name(product.tax_rate, taxes),
"priceRange": price_range_as_dict(availability.price_range),
"priceRangeUndiscounted": price_range_as_dict(
availability.price_range_undiscounted
diff --git a/saleor/product/views.py b/saleor/product/views.py
index fb0ea42d1e..cb8951adc7 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -83,7 +83,7 @@ def product_details(request, slug, product_id, form=None):
)
product_images = get_product_images(product)
variant_picker_data = get_variant_picker_data(
- product, request.discounts, request.country, request.currency
+ product, request.discounts, request.taxes, request.currency, request.country
)
product_attributes = get_product_attributes_data(product)
# show_variant_picker determines if variant picker is used or select input