Skip to content

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

Extend checkout to use discounts

Michał Ociepka931b078161ce
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index eaf6769576..0a0f559284 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -4,7 +4,7 @@ from functools import wraps
from django.conf import settings
from django.db import transaction
from django.forms.models import model_to_dict
-from prices import FixedDiscount, Price
+from prices import Price, FixedDiscount
from ..cart import Cart
from ..core import analytics
@@ -132,6 +132,50 @@ class Checkout(object):
self.storage['billing_address'] = address_data
self.modified = True
+ @property
+ def discount(self):
+ value = self.storage.get('discount_value')
+ currency = self.storage.get('discount_currency')
+ name = self.storage.get('discount_name')
+ if value is not None and name is not None and currency is not None:
+ amount = Price(value, currency=currency)
+ return FixedDiscount(amount, name)
+
+ @discount.setter
+ def discount(self, discount):
+ amount = discount.amount
+ self.storage['discount_value'] = str(amount.net)
+ self.storage['discount_currency'] = amount.currency
+ self.storage['discount_name'] = discount.name
+ self.modified = True
+
+ @discount.deleter
+ def discount(self):
+ if 'discount_value' in self.storage:
+ del self.storage['discount_value']
+ self.modified = True
+ if 'discount_currency' in self.storage:
+ del self.storage['discount_currency']
+ self.modified = True
+ if 'discount_name' in self.storage:
+ del self.storage['discount_name']
+ self.modified = True
+
+ @property
+ def voucher_code(self):
+ return self.storage.get('voucher_code')
+
+ @voucher_code.setter
+ def voucher_code(self, voucher_code):
+ self.storage['voucher_code'] = voucher_code
+ self.modified = True
+
+ @voucher_code.deleter
+ def voucher_code(self):
+ if 'voucher_code' in self.storage:
+ del self.storage['voucher_code']
+ self.modified = True
+
@property
def is_shipping_same_as_billing(self):
return Address.objects.are_identical(self.shipping_address, self.billing_address)
@@ -144,6 +188,7 @@ class Checkout(object):
address.save()
return address
+
@transaction.atomic
def create_order(self):
if self.is_shipping_required:
@@ -193,7 +238,7 @@ class Checkout(object):
total
for shipment, shipping_cost, total in self.deliveries)
total = sum(cost_iterator, zero)
- return total
+ return total if self.discount is None else self.discount.apply(total)
def get_total_shipping(self):
zero = Price(0, currency=settings.DEFAULT_CURRENCY)
@@ -203,11 +248,6 @@ class Checkout(object):
total = sum(cost_iterator, zero)
return total
- def get_discount(self):
- return FixedDiscount(
- amount=Price(-10, currency=settings.DEFAULT_CURRENCY),
- name='$10 OFF')
-
def load_checkout(view):
@wraps(view)