Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Set initial tax rate on product form
Anna Siwik7ee820e4f53f
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex 9af4a4d263..9e9ae4de35 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -121,7 +121,7 @@ def get_taxes_for_country(country):def apply_tax_to_price(taxes, rate_name, base):- if not taxes:+ if not taxes or not rate_name:# Naively convert Money to TaxedMoney for consistency with price# handling logic across the codebase, passthrough other money typesif isinstance(base, Money):diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex 5c4e183840..cf63c5f1c6 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -57,10 +57,12 @@ class ProductTypeSelectorForm(forms.Form):def get_tax_rate_type_choices():- rate_types = get_tax_rate_types() + [DEFAULT_TAX_RATE_NAME]- return [- (rate_type, VAT_RATE_TYPE_TRANSLATIONS.get(rate_type, rate_type))- for rate_type in rate_types]+ rate_types = get_tax_rate_types() + [DEFAULT_TAX_RATE_NAME, '']+ choices = [+ (rate_name, VAT_RATE_TYPE_TRANSLATIONS.get(rate_name, '---------'))+ for rate_name in rate_types]+ # sort choices alphabetically by translations+ return sorted(choices, key=lambda x: x[1])class ProductTypeForm(forms.ModelForm):@@ -230,6 +232,7 @@ class ProductForm(forms.ModelForm, AttributesMixin):def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)product_type = self.instance.product_type+ self.initial['tax_rate'] = product_type.tax_rateself.available_attributes = (product_type.product_attributes.prefetch_related('values').all())self.prepare_fields_for_attributes()diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 875bfa8619..026d5aa191 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -216,10 +216,9 @@ def product_toggle_is_published(request, pk):def product_edit(request, pk):product = get_object_or_404(Product.objects.prefetch_related('variants'), pk=pk)-- edit_variant = not product.product_type.has_variantsform = forms.ProductForm(request.POST or None, instance=product)+ edit_variant = not product.product_type.has_variantsif edit_variant:variant = product.variants.first()variant_form = forms.ProductVariantForm(diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 3afaa72035..4a53acfcfe 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -131,10 +131,9 @@ def attach_order_to_user(order, user):def get_tax_rate_by_name(rate_name, taxes=None):"""Return value of tax rate for current taxes."""- if not taxes:- return 0-- if rate_name in taxes:+ if not taxes or not rate_name:+ tax_rate = 0+ elif rate_name in taxes:tax_rate = taxes[rate_name]['value']else:tax_rate = taxes[DEFAULT_TAX_RATE_NAME]['value']diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 19c5d99bde..7160bd40f9 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -108,7 +108,8 @@ class Product(SeoModel):updated_at = models.DateTimeField(auto_now=True, null=True)is_featured = models.BooleanField(default=False)charge_taxes = models.BooleanField(default=True)- tax_rate = models.CharField(max_length=128, default='standard', blank=True)+ tax_rate = models.CharField(+ max_length=128, default=DEFAULT_TAX_RATE_NAME, blank=True)objects = ProductQuerySet.as_manager()