Skip to content

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

Return partial when returning shipping price

Mira Szewczyk5e78fc46ba68
diff --git a/saleor/cart/utils.py b/saleor/cart/utils.py
index 5726fdf0bb..e7e6dae1c2 100644
--- a/saleor/cart/utils.py
+++ b/saleor/cart/utils.py
@@ -8,10 +8,12 @@ from django.contrib import messages
from django.db import transaction
from django.utils.timezone import now
from django.utils.translation import pgettext_lazy
+from prices import PriceRange
from satchless.item import InsufficientStock
from . import CartStatus
from .models import Cart
+from ..core.utils import to_local_currency
COOKIE_NAME = 'cart'
@@ -212,3 +214,23 @@ def get_or_empty_db_cart(cart_queryset=Cart.objects.all()):
return view(request, cart, *args, **kwargs)
return func
return get_cart
+
+
+def get_cart_data(cart, shipping_range, currency, discounts):
+ cart_total = None
+ local_cart_total = None
+ shipping_required = False
+ total_with_shipment = None
+ if cart:
+ cart_total = cart.get_total(discounts=discounts)
+ local_cart_total = to_local_currency(cart_total, currency)
+ shipping_required = cart.is_shipping_required()
+ total_with_shipment = PriceRange(cart_total)
+ if shipping_required and shipping_range:
+ total_with_shipment = shipping_range + cart_total
+
+ return {
+ 'cart_total': cart_total,
+ 'local_cart_total': local_cart_total,
+ 'shipping_required': shipping_required,
+ 'total_with_shipment': total_with_shipment}
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 7897dcef6d..f76a28bbb2 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -11,7 +11,8 @@ from ..product.models import ProductVariant
from ..shipping.utils import get_shipment_options
from .forms import ReplaceCartLineForm, CountryForm
from .models import Cart
-from .utils import check_product_availability_and_warn, get_or_empty_db_cart
+from .utils import (check_product_availability_and_warn, get_or_empty_db_cart,
+ get_cart_data)
@get_or_empty_db_cart(cart_queryset=Cart.objects.for_display())
@@ -30,37 +31,35 @@ def index(request, cart):
'get_total': line.get_total(discounts=discounts),
'form': form})
- cart_total = None
- local_cart_total = None
- if cart:
- cart_total = cart.get_total(discounts=discounts)
- local_cart_total = to_local_currency(cart_total, request.currency)
-
default_country = get_user_shipping_country(request)
country_form = CountryForm(initial={'country': default_country})
default_country_options = get_shipment_options(default_country)
+ cart_data = get_cart_data(
+ cart, default_country_options, request.currency, request.discounts)
+ ctx = {
+ 'cart_lines': cart_lines,
+ 'country_form': country_form,
+ 'default_country_options': default_country_options}
+ ctx.update(cart_data)
+
return TemplateResponse(
- request, 'cart/index.html',
- {
- 'shipping_required': cart.is_shipping_required(),
- 'cart_lines': cart_lines,
- 'cart_total': cart_total,
- 'local_cart_total': local_cart_total,
- 'country_form': country_form,
- 'default_country_options': default_country_options})
+ request, 'cart/index.html', ctx)
-def get_shipping_options(request):
+@get_or_empty_db_cart(cart_queryset=Cart.objects.for_display())
+def get_shipping_options(request, cart):
country_form = CountryForm(request.POST or None)
if country_form.is_valid():
shipments = country_form.get_shipment_options()
- if shipments:
- return TemplateResponse(
- request, 'product/_price_range.html',
- {'price_range': shipments})
- else:
- return JsonResponse({'empty': True})
+ ctx = {
+ 'default_country_options': shipments,
+ 'country_form': country_form}
+ cart_data = get_cart_data(
+ cart, shipments, request.currency, request.discounts)
+ ctx.update(cart_data)
+ return TemplateResponse(
+ request, 'cart/_subtotal_table.html', ctx)
@get_or_empty_db_cart()
diff --git a/saleor/static/js/storefront.js b/saleor/static/js/storefront.js
index dc71283973..a07a2d88ff 100644
--- a/saleor/static/js/storefront.js
+++ b/saleor/static/js/storefront.js
@@ -196,10 +196,10 @@ $('.product-form button').click((e) => {
let $deliveryForm = $('.deliveryform');
let crsfToken = $deliveryForm.data('crsf');
-let $countrySelect = $('#id_country');
-let $newPrice = $('.cart__delivery-info__price');
-$countrySelect.on('change', (e) => {
- let newCountry = $countrySelect.val();
+let countrySelect = '#id_country';
+let $cartSubtotal = $('.cart__subtotal');
+$cartSubtotal.on('change', countrySelect, (e) => {
+ let newCountry = $(countrySelect).val();
$.ajax({
url: '/cart/shipingoptions/',
type: 'POST',
@@ -208,11 +208,7 @@ $countrySelect.on('change', (e) => {
'country': newCountry
},
success: (data) => {
- if(!data.empty){
- $newPrice.html(data);
- } else {
- $newPrice.empty();
- }
+ $cartSubtotal.html(data);
}
});
});
diff --git a/templates/cart/_subtotal_table.html b/templates/cart/_subtotal_table.html
new file mode 100644
index 0000000000..c745c4ef3e
--- /dev/null
+++ b/templates/cart/_subtotal_table.html
@@ -0,0 +1,69 @@
+{% load i18n %}
+{% load price_range from price_ranges %}
+{% load gross from prices_i18n %}
+
+{% if shipping_required %}
+ <div class="table__row">
+ <div class="row">
+ <div class="col-8 cart__total__subtotal">
+ <h3>{% trans "Subtotal" context "Order subtotal" %}</h3>
+ </div>
+ <div class="col-4">
+ <p class="cart-total text-right"
+ data-value="{% gross cart_total %}"
+ {% if local_cart_total %}data-value-local="{% gross local_cart_total %}"{% endif %}>
+ {% spaceless %}
+ {% gross cart_total html=True %}
+ {% if local_cart_total %}
+ <br>
+ <small>
+ &asymp;
+ {% gross local_cart_total html=True %}
+ </small>
+ {% endif %}
+ {% endspaceless %}
+ </p>
+ </div>
+ </div>
+ </div>
+ <div class="last table__row">
+ <div class="row">
+ <div class="col-7 cart__total__subtotal deliveryform" data-csrf="{{ csrf_token }}">
+ <h3>{% trans "Shipping cost for:" context "Cart secondary title" %}</h3>
+ {{ country_form.country }}
+ </div>
+ <div class="col-5">
+ <div class="row">
+ <div class="cart-item-subtotal col-md-12 col-12">
+ <h3 class="text-right cart__delivery-info__price">
+ {% if default_country_options %}
+ {% price_range default_country_options %}
+ {% endif %}
+ </h3>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+{% endif %}
+<div class="cart__total">
+ <div class="row">
+ <div class="col-8 cart__total__subtotal">
+ <h3>{% trans "Total" context "Order subtotal" %}</h3>
+ </div>
+ <div class="col-4">
+ <p class="cart-total">
+ {% spaceless %}
+ <h3 class="text-right">{% price_range total_with_shipment %}</h3>
+ {% if total_with_shipment %}
+ <br>
+ <h3 class="text-right"><small>
+ &asymp;
+ {% price_range total_with_shipment %}
+ </small></h3>
+ {% endif %}
+ {% endspaceless %}
+ </p>
+ </div>
+ </div>
+</div>
\ No newline at end of file
diff --git a/templates/cart/index.html b/templates/cart/index.html
index a236010d90..555cc01d31 100644
--- a/templates/cart/index.html
+++ b/templates/cart/index.html
@@ -4,7 +4,6 @@
{% load staticfiles %}
{% load render_bundle from webpack_loader %}
{% load get_thumbnail from product_images %}
-{% load price_range from price_ranges %}
{% block title %}{% trans "Your cart" context "Cart page title" %} — {{ block.super }}{% endblock %}
@@ -69,72 +68,8 @@
</div>
</div>
{% endfor %}
- {% if shipping_required %}
- <div class="table__row">
- <div class="row">
- <div class="col-8 cart__total__subtotal">
- <h3>{% trans "Subtotal" context "Order subtotal" %}</h3>
- </div>
- <div class="col-4">
- <p class="cart-total text-right"
- data-value="{% gross cart_total %}"
- {% if local_cart_total %}data-value-local="{% gross local_cart_total %}"{% endif %}>
- {% spaceless %}
- {% gross cart_total html=True %}
- {% if local_cart_total %}
- <br>
- <small>
- &asymp;
- {% gross local_cart_total html=True %}
- </small>
- {% endif %}
- {% endspaceless %}
- </p>
- </div>
- </div>
- </div>
- <div class="cart__line last table__row">
- <div class="row">
- <div class="col-7 cart__total__subtotal deliveryform" data-csrf="{{ csrf_token }}">
- <h3>{% trans "Shipping cost for:" context "Cart secondary title" %}</h3>
- {{ country_form.country }}
- </div>
- <div class="col-5">
- <div class="row">
- <div class="cart-item-subtotal col-md-12 col-12">
- <h3 class="text-right cart__delivery-info__price">
- {% if default_country_options %}
- {% price_range default_country_options %}
- {% endif %}
- </h3>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endif %}
- <div class="cart__total">
- <div class="row">
- <div class="col-8 cart__total__subtotal">
- <h3>{% trans "Total" context "Order subtotal" %}</h3>
- </div>
- <div class="col-4">
- <p class="cart-total text-right"
- data-value="{% gross cart_total %}"
- {% if local_cart_total %}data-value-local="{% gross local_cart_total %}"{% endif %}>
- {% spaceless %}
- {% gross cart_total html=True %}
- {% if local_cart_total %}
- <br>
- <small>
- &asymp;
- {% gross local_cart_total html=True %}
- </small>
- {% endif %}
- {% endspaceless %}
- </p>
- </div>
- </div>
+ <div class="cart__subtotal">
+ {% include 'cart/_subtotal_table.html' %}
</div>
<div class="row">
<div class="col-md-12">
diff --git a/tests/test_cart.py b/tests/test_cart.py
index 6181f9e558..922b61b588 100644
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@ -8,7 +8,6 @@ from babeldjango.templatetags.babel import currencyfmt
from django.contrib.auth.models import AnonymousUser
from django.core import signing
from django.core.exceptions import ObjectDoesNotExist
-from django.core.urlresolvers import reverse
from django.http import HttpResponse
from mock import MagicMock, Mock
from prices import Price
@@ -20,6 +19,7 @@ from saleor.cart.models import Cart, ProductGroup
from saleor.cart.views import update
from saleor.discount.models import Sale
from saleor.product.models import Category
+from saleor.shipping.utils import get_shipment_options
@pytest.fixture()
@@ -718,3 +718,21 @@ def test_get_or_create_db_cart(customer_user, db, rf):
request.user = AnonymousUser()
decorated_view(request)
assert Cart.objects.filter(user__isnull=True).count() == 1
+
+
+def test_get_cart_data(request_cart_with_item, shipping_method):
+ shippment_option = get_shipment_options('PL')
+ cart_data = utils.get_cart_data(
+ request_cart_with_item, shippment_option, 'USD', None)
+ assert cart_data['cart_total'] == Price(net=10, currency='USD')
+ assert cart_data['total_with_shipment'].min_price == Price(
+ net=20, currency='USD')
+
+
+def test_get_cart_data_no_shipping(request_cart_with_item):
+ shippment_option = get_shipment_options('PL')
+ cart_data = utils.get_cart_data(
+ request_cart_with_item, shippment_option, 'USD', None)
+ cart_total = cart_data['cart_total']
+ assert cart_total == Price(net=10, currency='USD')
+ assert cart_data['total_with_shipment'].min_price == cart_total