Skip to content

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

Adjust product details UI to handle product classes that doesn't require variants

Marcin Gębala4504420f333d
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index b9fc305381..b616c38876 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -135,7 +135,7 @@ def product_create(request, class_pk):
@staff_member_required
def product_detail(request, pk):
products = Product.objects.prefetch_related(
- 'variants', 'images',
+ 'variants__stock', 'images',
'product_class__variant_attributes__values').all()
product = get_object_or_404(products, pk=pk)
variants = product.variants.all()
@@ -143,11 +143,22 @@ def product_detail(request, pk):
availability = get_availability(product)
sale_price = availability.price_range
gross_price_range = product.get_gross_price_range()
- requires_variants = product.product_class.has_variants
+
+ # no_variants is True for product classes that doesn't require variant.
+ # In this case we're using the first variant under the hood to allow stock
+ # management.
+ no_variants = not product.product_class.has_variants
+ only_variant = variants.first() if no_variants else None
+ if only_variant:
+ stock = only_variant.stock.all()
+ else:
+ stock = Stock.objects.none()
+
ctx = {
'product': product, 'sale_price': sale_price, 'variants': variants,
'gross_price_range': gross_price_range, 'images': images,
- 'requires_variants': requires_variants}
+ 'no_variants': no_variants, 'only_variant': only_variant,
+ 'stock': stock}
return TemplateResponse(
request, 'dashboard/product/product_detail.html', ctx)
@@ -179,6 +190,8 @@ def product_edit(request, pk):
if form.is_valid() and not variant_errors:
product = form.save()
+ if edit_variant:
+ variant_form.save()
msg = pgettext_lazy(
'Dashboard message', 'Updated product %s') % product
messages.success(request, msg)
@@ -338,6 +351,13 @@ def variant_details(request, product_pk, variant_pk):
product = get_object_or_404(Product, pk=product_pk)
qs = product.variants.prefetch_related('images', 'stock')
variant = get_object_or_404(qs, pk=variant_pk)
+
+ # If the product class of this product assumes no variants, redirect to
+ # product details page that has special UI for products without variants.
+
+ if not product.product_class.has_variants:
+ return redirect('dashboard:product-detail', pk=product.pk)
+
stock = variant.stock.all()
images = variant.images.all()
ctx = {
diff --git a/templates/dashboard/product/product_detail.html b/templates/dashboard/product/product_detail.html
index 6bc82d7bce..c27e3247fb 100644
--- a/templates/dashboard/product/product_detail.html
+++ b/templates/dashboard/product/product_detail.html
@@ -35,7 +35,7 @@
<div class="col s12 l9">
<div class="card">
<div class="card-content">
- <span class="card-title">{{ product }}</span>
+ <span class="card-title">{{ product }} {% if no_variants and only_variant %}<br> {% trans "SKU" context "Dashboard product details view" %} {{ only_variant.sku }}{% endif %}</span>
<div class="row">
<div class="col s5 m6">
<p class="text-padding">{% trans "Sale price" context "Product field" %}</p>
@@ -85,42 +85,82 @@
</a>
</div>
</div>
- <div class="card">
- <div class="card-content">
- <span class="card-title">{% trans "Variant" %}</span>
- </div>
- <div class="data-table-header-action">
- <a href="{% url 'dashboard:variant-add' product_pk=product.pk %}" class="btn-data-table btn-flat">{% trans "Add" %}</a>
- </div>
- <div class="data-table-container">
- <table class="bordered highlight responsive data-table">
- <thead>
- <tr>
- <th>{% trans "SKU" %}</th>
- <th>{% trans "Name" %}</th>
- <th>{% trans "Status" %}</th>
- <th class="right-align">{% trans "Price" %}</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- {% for variant in variants %}
- <tr data-action-go="{% url "dashboard:variant-details" product_pk=product.pk variant_pk=variant.pk %}">
- <td>{{ variant.sku }}</td>
- <td>{{ variant }}</td>
- <td class="wide"></td>
- <td class="right-align">{% gross variant.get_price_per_item %}</td>
- <td><a href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}" class="btn btn-flat">{% trans "Edit" %}</a></td>
+ {% if not no_variants %}
+ <div class="card">
+ <div class="card-content">
+ <span class="card-title">{% trans "Variant" %}</span>
+ </div>
+ <div class="data-table-header-action">
+ <a href="{% url 'dashboard:variant-add' product_pk=product.pk %}" class="btn-data-table btn-flat">{% trans "Add" %}</a>
+ </div>
+ <div class="data-table-container">
+ <table class="bordered highlight responsive data-table">
+ <thead>
+ <tr>
+ <th>{% trans "SKU" %}</th>
+ <th>{% trans "Name" %}</th>
+ <th>{% trans "Status" %}</th>
+ <th class="right-align">{% trans "Price" %}</th>
+ <th></th>
</tr>
- {% empty %}
+ </thead>
+ <tbody>
+ {% for variant in variants %}
+ <tr data-action-go="{% url "dashboard:variant-details" product_pk=product.pk variant_pk=variant.pk %}">
+ <td>{{ variant.sku }}</td>
+ <td>{{ variant }}</td>
+ <td class="wide">{% render_variant_availability_status variant %}</td>
+ <td class="right-align">{% gross variant.get_price_per_item %}</td>
+ <td><a href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}" class="btn btn-flat">{% trans "Edit" %}</a></td>
+ </tr>
+ {% empty %}
+ <tr>
+ <td colspan="5">{% trans "No variants found" context "Dashboard product details page" %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
+ </div>
+ {% endif %}
+ {% if no_variants and only_variant %}
+ <div class="card">
+ <div class="card-content">
+ <span class="card-title">{% trans "Stock" context "Dashboard variant details view" %}</span>
+ </div>
+ <div class="data-table-header-action">
+ <a class="btn btn-flat" href="{% url "dashboard:variant-stock-add" product_pk=product.pk variant_pk=only_variant.pk %}">
+ {% trans "Add" context "Dashboard action" %}
+ </a>
+ </div>
+ <div class="data-table-container">
+ <table class="bordered highlight responsive data-table">
+ <thead>
<tr>
- <td colspan="5">{% trans "No variants found" context "Dashboard product details page" %}</td>
+ <th class="wide">{% trans "Location" %}</th>
+ <th>{% trans "Quantity" %}</th>
+ <th>{% trans "Allocated" %}</th>
+ <th class="right-align">{% trans "Cost" %}</th>
</tr>
- {% endfor %}
- </tbody>
- </table>
+ </thead>
+ <tbody>
+ {% for stock_item in stock %}
+ <tr data-action-go="{% url "dashboard:variant-stock-details" product_pk=product.pk variant_pk=only_variant.pk stock_pk=stock_item.pk %}">
+ <td class="wide">{{ stock_item.location }}</td>
+ <td>{{ stock_item.quantity }}</td>
+ <td>{{ stock_item.quantity_allocated }}</td>
+ <td class="right-align">{% if stock_item.cost_price %}{% gross stock_item.cost_price %}{% else %}-{% endif %}</td>
+ </tr>
+ {% empty %}
+ <tr>
+ <td colspan="4">{% trans "No stock records found" context "Dashboard variant details page" %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
</div>
- </div>
+ {% endif %}
</div>
<div class="col s12 l3">
<div id="status">