Skip to content

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

Add DeliveryGroup, Shipping and partitioner

Michal Ociepka2f16073d2605
diff --git a/cart/__init__.py b/cart/__init__.py
index 8eadb69ade..f1744950ce 100755
--- a/cart/__init__.py
+++ b/cart/__init__.py
@@ -1,6 +1,37 @@
from django.conf import settings
from django.utils.translation import ugettext
+from prices import Price
from satchless import cart
+from satchless.item import ItemSet, ItemLine, Item, ItemRange
+
+
+class Shipping(Item):
+
+ def get_price_per_item(self, **kwargs):
+ return Price(5, currency=settings.SATCHLESS_DEFAULT_CURRENCY)
+
+
+class DeliveryGroup(ItemSet):
+
+ items = None
+
+ def __init__(self, items):
+ self.items = items or []
+
+ def __iter__(self):
+ for i in self.items:
+ yield i
+
+ def get_delivery_methods(self, **kwargs):
+ yield Shipping(**kwargs)
+
+ def get_min_delivery_method(self, **kwargs):
+ return min(self.get_delivery_methods(),
+ key=lambda x: x.get_price_per_item(**kwargs))
+
+
+def cart_partition(cart):
+ yield DeliveryGroup(list(cart))
class InsufficientStockException(Exception):
diff --git a/cart/forms.py b/cart/forms.py
index 894214983d..54a6e3e738 100644
--- a/cart/forms.py
+++ b/cart/forms.py
@@ -15,7 +15,7 @@ class QuantityField(forms.DecimalField):
class AddToCartForm(forms.Form):
- quantity = QuantityField(pgettext('Form field', 'quantity'))
+ quantity = QuantityField(label=pgettext('Form field', 'quantity'))
error_messages = {
'insufficient-stock': ugettext(
'Only %(remaining)d remaining in stock.')}
diff --git a/cart/templates/cart/index.html b/cart/templates/cart/index.html
index 9591867e6b..2ffcabb6e5 100644
--- a/cart/templates/cart/index.html
+++ b/cart/templates/cart/index.html
@@ -28,7 +28,7 @@
</tbody>
<tfoot>
<tr>
- <td colspan="2"></td>
+ <td colspan="2">Shipping: {% gross shipping %}</td>
<td><button class="btn" type="submit">Update</button></td>
<td>Total:</td>
<td>{% gross cart.get_total %}</td>
diff --git a/cart/views.py b/cart/views.py
index 2c03cc2388..ab83494d86 100755
--- a/cart/views.py
+++ b/cart/views.py
@@ -1,10 +1,17 @@
from . import get_cart_from_request
from .forms import ReplaceCartLineFormSet
+from cart import cart_partition
from django.shortcuts import redirect
from django.template.response import TemplateResponse
+from prices import Price
+from django.conf import settings
def index(request):
cart = get_cart_from_request(request)
+ cart_deliveries = cart_partition(cart)
+ cart_delivery_prices = [delivery.get_min_delivery_method().get_price()
+ for delivery in cart_deliveries]
+ zero = Price(0, currency=settings.SATCHLESS_DEFAULT_CURRENCY)
formset = ReplaceCartLineFormSet(request.POST or None, cart=cart)
@@ -14,5 +21,6 @@ def index(request):
return TemplateResponse(request, 'cart/index.html', {
'cart': cart,
- 'formset': formset
+ 'formset': formset,
+ 'shipping': sum(cart_delivery_prices, zero)
})