Skip to content

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

Improve product details page

Anna Siwik1472ec7fdfd9
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 3fca8665cf..875bfa8619 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -180,8 +180,10 @@ def product_details(request, pk):
product = get_object_or_404(products, pk=pk)
variants = product.variants.all()
images = product.images.all()
- availability = get_availability(product)
- sale_price = availability.price_range
+ availability = get_availability(
+ product, discounts=request.discounts, taxes=request.taxes)
+ sale_price = availability.price_range_undiscounted
+ discounted_price = availability.price_range
purchase_cost, gross_margin = get_product_costs_data(product)
# no_variants is True for product types that doesn't require variant.
@@ -190,7 +192,8 @@ def product_details(request, pk):
no_variants = not product.product_type.has_variants
only_variant = variants.first() if no_variants else None
ctx = {
- 'product': product, 'sale_price': sale_price, 'variants': variants,
+ 'product': product, 'sale_price': sale_price,
+ 'discounted_price': discounted_price, 'variants': variants,
'images': images, 'no_variants': no_variants,
'only_variant': only_variant, 'purchase_cost': purchase_cost,
'gross_margin': gross_margin, 'is_empty': not variants.exists()}
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index bd31b05339..38b70f086b 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -129,18 +129,10 @@ def attach_order_to_user(order, user):
order.save(update_fields=['user'])
-def get_variant_tax_rate(variant, taxes=None):
- """Return value of variant tax rate for current taxes."""
- if not taxes:
- return 0
-
- rate_name = variant.product.tax_rate
- if rate_name in taxes:
- tax = taxes[rate_name]
- else:
- tax = taxes[DEFAULT_TAX_RATE_NAME]
-
- return tax['value']
+def get_tax_rate_by_name(rate_name, taxes=None):
+ """Return value of tax rate for current taxes."""
+ tax = taxes.get(rate_name) or taxes.get(DEFAULT_TAX_RATE_NAME)
+ return tax['value'] if tax else 0
def add_variant_to_order(
@@ -171,7 +163,7 @@ def add_variant_to_order(
quantity=quantity_not_fulfilled,
unit_price=price,
variant=variant,
- tax_rate=get_variant_tax_rate(variant, taxes))
+ tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))
allocate_stock(variant, quantity_not_fulfilled)
diff --git a/saleor/product/utils/variants_picker.py b/saleor/product/utils/variants_picker.py
index 6c680cd941..f0351d0ed2 100644
--- a/saleor/product/utils/variants_picker.py
+++ b/saleor/product/utils/variants_picker.py
@@ -4,6 +4,7 @@ from django.contrib.sites.models import Site
from django_prices.templatetags import prices_i18n
from ...core.utils import to_local_currency
+from ...order.utils import get_tax_rate_by_name
from ...seo.schema.product import variant_json_ld
from .availability import get_availability
@@ -57,6 +58,7 @@ def get_variant_picker_data(
data['availability'] = {
'discount': price_as_dict(availability.discount),
+ '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/static/js/components/variantPicker/VariantPrice.js b/saleor/static/js/components/variantPicker/VariantPrice.js
index 7cbd1acf83..25fbcb0069 100644
--- a/saleor/static/js/components/variantPicker/VariantPrice.js
+++ b/saleor/static/js/components/variantPicker/VariantPrice.js
@@ -12,6 +12,7 @@ export default observer(class VariantPrice extends Component {
let priceText, priceUndiscountedText, priceLocalCurrency, isDiscount;
const { availability, store, displayGrossPrices } = this.props;
const variant = store.variant;
+ const taxRate = availability.taxRate;
if (!store.isEmpty) {
// variant price
isDiscount = variant.price.gross !== variant.priceUndiscounted.gross;
@@ -57,6 +58,12 @@ export default observer(class VariantPrice extends Component {
{priceLocalCurrency && (
<p><small className="text-info">&asymp; {priceLocalCurrency}</small></p>
)}
+ {displayGrossPrices && (
+ <small>including {taxRate}% VAT</small>
+ )}
+ {!displayGrossPrices && (
+ <small>excluding {taxRate}% VAT</small>
+ )}
</h2>
);
}
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index abec90e0da..0a9edf7bb8 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -2,6 +2,7 @@
{% load i18n %}
{% load prices_i18n %}
+{% load price_ranges %}
{% load staticfiles %}
{% load status %}
{% load utils %}
@@ -33,44 +34,40 @@
{% endblock %}
{% block content %}
<div class="row">
- <div class="col s12 l9">
+ <div class="col s12 l8">
<div class="card">
<div class="card-content">
- <span class="card-title">{{ product }} <br> {{ product.product_type }}</span>
+ <span class="card-title">
+ {{ product }}
+ </span>
<div class="row">
- <div class="col s5 m6">
- <p class="text-padding">
- {% if product.charge_taxes %}
- {% trans "Sale price (taxes are charged)" context "Product field" %}
- {% else %}
- {% trans "Sale price (taxes are not charged)" context "Product field" %}
- {% endif %}
- </p>
- </div>
- <div class="col s7 m6">
- {% with sale_price as price_range %}
- <p class="text-padding right-align">{{ price_range.start.gross|amount }} - {{ price_range.stop.gross|amount }}</p>
- {% endwith %}
- </div>
- <div class="col s5 m6">
- <p class="text-padding">{% trans "Purchase cost" context "Product field" %}</p>
- </div>
- <div class="col s7 m6">
- {% with purchase_cost as price_range %}
- <p class="text-padding right-align">{{ price_range.start.gross|amount }} - {{ price_range.stop.gross|amount }}</p>
- {% endwith %}
- </div>
- <div class="col s5 m6">
- <p class="text-padding">{% trans "Gross margin" context "Product field" %}</p>
+ <div class="col s12">
+ <form id="toggle-publish-form" action="{% url 'dashboard:product-publish' product.pk %}">
+ {% csrf_token %}
+ <div class="switch" id="product-is-published">
+ <label>
+ <input id="toggle-publish-switch" type="checkbox" {% if product.is_published %} checked {% endif %}>
+ <span class="lever"></span>
+ <span class="label">
+ {% if product.is_published %}
+ {% trans "Published" context "Product field" %}
+ {% else %}
+ {% trans "Draft" context "Product field" %}
+ {% endif %}
+ </span>
+ </label>
+ </div>
+ </form>
</div>
- <div class="col s7 m6">
- {% if gross_margin %}
- <p class="text-padding right-align">{{ gross_margin.0 }}% - {{ gross_margin.1 }}%</p>
- {% else %}
- <p class="text-padding right-align">-</p>
- {% endif %}
+ </div>
+ <div class="row">
+ <div class="col s12">
+ <h5>{% render_availability_status product %}</h5>
</div>
</div>
+ <p>
+ {{ product.description }}
+ </p>
</div>
<div class="card-action">
{% if perms.product.edit_product %}
@@ -143,29 +140,95 @@
{% endif %}
</div>
{% if perms.product.edit_product %}
- <div class="col s12 l3">
+ <div class="col s12 l4">
<div id="status">
<div class="row no-margin">
<div class="col s12">
<div class="card">
- <div class="card-content">
- <form id="toggle-publish-form" action="{% url 'dashboard:product-publish' product.pk %}">
- <div class="switch" id="product-is-published">
- <label>
- <input id="toggle-publish-switch" type="checkbox" {% if product.is_published %} checked {% endif %}>
- <span class="lever"></span>
- <span class="label">
- {% if product.is_published %}
- {% trans "Published" context "Product field" %}
+ <div class="data-table-header">
+ <div class="data-table-title">
+ <h5>
+ {% trans "Pricing" context "Product pricing card header" %}
+ </h5>
+ <h6>
+ {% if product.charge_taxes %}
+ {% blocktrans trimmed with tax_rate=product.tax_rate context "Product pricing card header subtitle" %}
+ Taxes are charged ({{ tax_rate }} tax rate)
+ {% endblocktrans %}
+ {% else %}
+ {% trans "Taxes are not charged" context "Product pricing card header subtitle" %}
+ {% endif %}
+ </h6>
+ </div>
+ </div>
+ <div class="data-table-container">
+ <table class="data-table bordered highlight">
+ <tbody>
+ <tr>
+ <td>
+ {% trans "Sale price" context "Product field" %}
+ </td>
+ <td class="right-align">
+ {% price_to_display sale_price.start as start_price %}
+ {% price_to_display sale_price.stop as stop_price %}
+
+ {% if start_price == stop_price %}
+ {{ start_price|amount }}
{% else %}
- {% trans "Draft" context "Product field" %}
+ {{ start_price|amount }} - {{ stop_price|amount }}
{% endif %}
- </span>
- </label>
- </div>
- {% csrf_token %}
- </form>
- <h5>{% render_availability_status product %}</h5>
+ </td>
+ </tr>
+ {% if discounted_price != sale_price %}
+ <tr>
+ <td>
+ {% trans "Discounted price" context "Product field" %}
+ </td>
+ <td class="right-align">
+ {% price_to_display discounted_price.start as start_price %}
+ {% price_to_display discounted_price.stop as stop_price %}
+
+ {% if start_price == stop_price %}
+ {{ start_price|amount }}
+ {% else %}
+ {{ start_price|amount }} - {{ stop_price|amount }}
+ {% endif %}
+ </td>
+ </tr>
+ {% endif %}
+ <tr>
+ <td>
+ {% trans "Purchase cost" context "Product field" %}
+ </td>
+ <td class="right-align">
+ {% price_to_display purchase_cost.start as start_price %}
+ {% price_to_display purchase_cost.stop as stop_price %}
+
+ {% if start_price == stop_price %}
+ {{ start_price|amount }}
+ {% else %}
+ {{ start_price|amount }} - {{ stop_price|amount }}
+ {% endif %}
+ </td>
+ </tr>
+ <tr>
+ <td>
+ {% trans "Gross margin" context "Product field" %}
+ </td>
+ <td class="right-align">
+ {% if gross_margin %}
+ {% if gross_margin.0 == gross_margin.1 %}
+ {{ gross_margin.0 }}%
+ {% else %}
+ {{ gross_margin.0 }}% - {{ gross_margin.1 }}%
+ {% endif %}
+ {% else %}
+ -
+ {% endif %}
+ </td>
+ </tr>
+ </tbody>
+ </table>
</div>
</div>
</div>
@@ -173,7 +236,7 @@
</div>
</div>
{% endif %}
- <div class="col s12 l3">
+ <div class="col s12 l4">
<div id="images">
<div class="row">
<div class="col s12">
diff --git a/templates/product/_price_range.html b/templates/product/_price_range.html
index c83869a3a2..818fffd835 100644
--- a/templates/product/_price_range.html
+++ b/templates/product/_price_range.html
@@ -4,7 +4,7 @@
<span>
<span>
{% if price_range.start != price_range.stop %}{% trans "from" context "product price range" %} {% endif %}
- {% if display_gross %}
+ {% if display_gross_prices %}
{{ price_range.start.gross|amount:'html' }}
{% else %}
{{ price_range.start.net|amount:'html' }}
diff --git a/tests/test_order.py b/tests/test_order.py
index 698d0f0441..557eae9c7e 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -11,7 +11,7 @@ from saleor.order.forms import OrderNoteForm
from saleor.order.models import Order
from saleor.order.utils import (
add_variant_to_order, cancel_fulfillment, cancel_order,
- get_variant_tax_rate, recalculate_order, restock_fulfillment_lines,
+ get_tax_rate_by_name, recalculate_order, restock_fulfillment_lines,
restock_order_lines, update_order_status)
@@ -37,27 +37,23 @@ def test_order_get_subtotal(order_with_lines):
assert order_with_lines.get_subtotal() == target_subtotal
-def test_get_variant_tax_rate(product, taxes):
- variant = product.variants.get()
-
- tax_rate = get_variant_tax_rate(variant, taxes)
-
- assert tax_rate == taxes[product.tax_rate]['value']
+def test_get_tax_rate_by_name(taxes):
+ rate_name = 'pharmaceuticals'
+ tax_rate = get_tax_rate_by_name(rate_name, taxes)
+ assert tax_rate == taxes[rate_name]['value']
-def test_get_variant_tax_rate_fallback_to_standard(product, taxes):
- product.tax_rate = 'unexisting tax rate'
- variant = product.variants.get()
-
- tax_rate = get_variant_tax_rate(variant, taxes)
- assert tax_rate == taxes['standard']['value']
+def test_get_tax_rate_by_name_fallback_to_standard(taxes):
+ rate_name = 'unexisting tax rate'
+ tax_rate = get_tax_rate_by_name(rate_name, taxes)
+ assert tax_rate == taxes[rate_name]['value']
-def test_get_variant_tax_rate_empty_taxes(product):
- variant = product.variants.get()
- tax_rate = get_variant_tax_rate(variant)
+def test_get_tax_rate_by_name_empty_taxes(product):
+ rate_name = 'unexisting tax rate'
+ tax_rate = get_tax_rate_by_name(rate_name)
assert tax_rate == 0