Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add DeliveryGroup, Shipping and partitioner
Michal Ociepka2f16073d2605
diff --git a/cart/__init__.py b/cart/__init__.pyindex 8eadb69ade..f1744950ce 100755--- a/cart/__init__.py+++ b/cart/__init__.py@@ -1,6 +1,37 @@from django.conf import settingsfrom django.utils.translation import ugettext+from prices import Pricefrom 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.pyindex 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.htmlindex 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.pyindex 2c03cc2388..ab83494d86 100755--- a/cart/views.py+++ b/cart/views.py@@ -1,10 +1,17 @@from . import get_cart_from_requestfrom .forms import ReplaceCartLineFormSet+from cart import cart_partitionfrom django.shortcuts import redirectfrom django.template.response import TemplateResponse+from prices import Price+from django.conf import settingsdef 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)})