Skip to content

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

Move input type determining to form from view

dominik-zeglen33346c62bf2b
diff --git a/saleor/product/forms.py b/saleor/product/forms.py
index efcde3bd92..6409b2517a 100644
--- a/saleor/product/forms.py
+++ b/saleor/product/forms.py
@@ -34,6 +34,10 @@ class ProductForm(AddToCartForm):
for vi in variant.variant_images.all()]
for variant in self.product.variants.all()}
variant_field.widget.attrs['data-images'] = json.dumps(images_map)
+ if variant_field.queryset.count() < 2:
+ variant_field.widget = forms.HiddenInput({
+ 'value': self.product.variants.all()[0].pk
+ })
def get_variant(self, cleaned_data):
return cleaned_data.get('variant')
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 8508129da4..c2d86e7113 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -65,9 +65,6 @@ def product_details(request, slug, product_id, form=None):
product_attributes = get_product_attributes_data(product)
# show_variant_picker determines if variant picker is used or select input
show_variant_picker = all([v.attributes for v in product.variants.all()])
- has_variants = product.variants.count() > 1
- # first_variant is used in hidden input if has_variants is False
- first_variant = product.variants.all()[0]
json_ld_data = product_json_ld(product, availability, product_attributes)
return TemplateResponse(
request, 'product/details.html',
@@ -81,9 +78,7 @@ def product_details(request, slug, product_id, form=None):
'variant_picker_data': json.dumps(
variant_picker_data, default=serialize_decimal),
'json_ld_product_data': json.dumps(
- json_ld_data, default=serialize_decimal),
- 'has_variants': has_variants,
- 'first_variant': first_variant})
+ json_ld_data, default=serialize_decimal)})
def product_add_to_cart(request, slug, product_id):
diff --git a/templates/product/details.html b/templates/product/details.html
index 87fa786d83..112963dcb9 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -136,11 +136,7 @@
<form id="product-form" role="form" class="product-form clearfix" method="post"
action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" novalidate>
{% csrf_token %}
- {% if has_variants %}
- {% bootstrap_field form.variant %}
- {% else %}
- <input type="hidden" value="{{ first_variant.pk }}" name="variant" id="id_variant" />
- {% endif %}
+ {% bootstrap_field form.variant %}
<div class="product__info__quantity">
{% bootstrap_field form.quantity %}
</div>