Skip to content

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

Display shipping price range in cart

Mira Szewczyk5cb96cd3b45e
diff --git a/saleor/cart/views.py b/saleor/cart/views.py
index 6ac72278a8..7897dcef6d 100644
--- a/saleor/cart/views.py
+++ b/saleor/cart/views.py
@@ -55,7 +55,12 @@ def get_shipping_options(request):
country_form = CountryForm(request.POST or None)
if country_form.is_valid():
shipments = country_form.get_shipment_options()
- return JsonResponse({'options': list(shipments)})
+ if shipments:
+ return TemplateResponse(
+ request, 'product/_price_range.html',
+ {'price_range': shipments})
+ else:
+ return JsonResponse({'empty': True})
@get_or_empty_db_cart()
diff --git a/saleor/shipping/utils.py b/saleor/shipping/utils.py
index e6cc4fde0b..0ae8b47531 100644
--- a/saleor/shipping/utils.py
+++ b/saleor/shipping/utils.py
@@ -1,10 +1,15 @@
+from prices import PriceRange
+
from .models import ShippingMethodCountry
def get_shipment_options(country_code):
shipping_methods_qs = ShippingMethodCountry.objects.select_related(
- 'shipping_method').values('price', 'shipping_method__name')
+ 'shipping_method')
shipping_methods = shipping_methods_qs.filter(country_code=country_code)
if not shipping_methods.exists():
shipping_methods = shipping_methods_qs.filter(country_code='')
- return shipping_methods
+ if shipping_methods:
+ shipping_methods = shipping_methods.values_list('price', flat=True)
+ return PriceRange(
+ min_price=min(shipping_methods), max_price=max(shipping_methods))
diff --git a/saleor/static/js/storefront.js b/saleor/static/js/storefront.js
index 6e9c652e95..dc71283973 100644
--- a/saleor/static/js/storefront.js
+++ b/saleor/static/js/storefront.js
@@ -197,7 +197,6 @@ $('.product-form button').click((e) => {
let $deliveryForm = $('.deliveryform');
let crsfToken = $deliveryForm.data('crsf');
let $countrySelect = $('#id_country');
-let $newMethod = $('.cart__delivery-info__method');
let $newPrice = $('.cart__delivery-info__price');
$countrySelect.on('change', (e) => {
let newCountry = $countrySelect.val();
@@ -209,12 +208,11 @@ $countrySelect.on('change', (e) => {
'country': newCountry
},
success: (data) => {
- $newMethod.empty();
- $newPrice.empty();
- $.each(data.options, (key, val) => {
- $newMethod.append('<p>' + val.shipping_method__name + '</p>');
- $newPrice.append('<p>$' + val.price[1] + '</p>');
- });
+ if(!data.empty){
+ $newPrice.html(data);
+ } else {
+ $newPrice.empty();
+ }
}
});
});