Skip to content

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

Improve discount implementation. Add apply_on field

Artur Smęta43f10607e89
diff --git a/saleor/core/middleware.py b/saleor/core/middleware.py
index 132891164f..13b3e7d6b9 100644
--- a/saleor/core/middleware.py
+++ b/saleor/core/middleware.py
@@ -25,5 +25,5 @@ class GoogleAnalytics(object):
class DiscountMiddleware(object):
def process_request(self, request):
discounts = Discount.objects.all()
- discounts = discounts.prefetch_related('products')
+ discounts = discounts.prefetch_related('products', 'categories')
request.discounts = discounts
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index f4b86fc7c4..b0a8580d15 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -7,7 +7,7 @@ from ...product.models import Discount
class DiscountForm(forms.ModelForm):
class Meta:
model = Discount
- fields = ['name', 'type', 'products', 'categories', 'value']
+ exclude = []
def clean(self):
cleaned_data = super(DiscountForm, self).clean()
diff --git a/saleor/product/migrations/0010_discount_apply_on.py b/saleor/product/migrations/0010_discount_apply_on.py
new file mode 100644
index 0000000000..5d3bafe787
--- /dev/null
+++ b/saleor/product/migrations/0010_discount_apply_on.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.1 on 2016-01-14 10:40
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('product', '0009_auto_20160112_1025'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='discount',
+ name='apply_on',
+ field=models.CharField(choices=[('products', 'Products'), ('categories', 'Categories'), ('both', 'Both products and categories')], default='both', max_length=10),
+ ),
+ ]
diff --git a/saleor/product/models/discounts.py b/saleor/product/models/discounts.py
index 1debc38e6d..5ccaa9d2aa 100644
--- a/saleor/product/models/discounts.py
+++ b/saleor/product/models/discounts.py
@@ -17,13 +17,24 @@ class NotApplicable(ValueError):
class Discount(models.Model):
FIXED = 'fixed'
PERCENTAGE = 'percentage'
+ APPLY_ON_PRODUCTS = 'products'
+ APPLY_ON_CATEGORIES = 'categories'
+ APPLY_ON_BOTH = 'both'
DISCOUNT_TYPE_CHOICES = (
(FIXED, pgettext_lazy('discount type', 'Fixed amount')),
(PERCENTAGE, pgettext_lazy('discount_type', 'Percentage discount')))
+ APPLY_ON_CHOICES = (
+ (APPLY_ON_PRODUCTS, pgettext_lazy('discount apply on', 'Products')),
+ (APPLY_ON_CATEGORIES, pgettext_lazy('discount apply on', 'Categories')),
+ (APPLY_ON_BOTH, pgettext_lazy('discount apply on',
+ 'Both products and categories')))
+
name = models.CharField(max_length=255)
type = models.CharField(max_length=10, choices=DISCOUNT_TYPE_CHOICES)
+ apply_on = models.CharField(max_length=10, choices=APPLY_ON_CHOICES,
+ default=APPLY_ON_BOTH)
products = models.ManyToManyField('Product', blank=True)
categories = models.ManyToManyField('Category', blank=True)
value = models.DecimalField(
@@ -48,18 +59,25 @@ class Discount(models.Model):
currency=settings.DEFAULT_CURRENCY)
return FixedDiscount(amount=discount_price, name=self.name)
elif self.type == self.PERCENTAGE:
- return percentage_discount(value=self.value,
- name=self.name)
- raise NotImplementedError()
+ return percentage_discount(value=self.value, name=self.name)
+ raise NotImplementedError('Unknown discount type')
def modifier_for_product(self, product):
- pk = product.pk
- categories = product.categories.all()
+ product_categories = product.categories.all()
+ discounted_categories = self.categories.all()
check_price = product.get_price_per_item()
- if self.products.exists() and not (self.products.filter(pk=pk).exists()):
- raise NotApplicable('Discount not applicable for this product')
- if self.categories.exists() and not any(c in self.categories.all() for c in categories):
- raise NotApplicable('Discount not applicable for this product')
+ product_in_discount = product in self.products.all()
+ category_in_discount = any(c in discounted_categories
+ for c in product_categories)
+
+ if self.apply_on == self.APPLY_ON_BOTH:
+ if not (product_in_discount or category_in_discount):
+ raise NotApplicable('Discount not applicable for this product')
+ else:
+ if self.apply_on == self.APPLY_ON_PRODUCTS and not product_in_discount:
+ raise NotApplicable('Discount not applicable for this product')
+ if self.apply_on == self.APPLY_ON_CATEGORIES and not category_in_discount:
+ raise NotApplicable('Discount not applicable for this product')
discount = self.get_discount()
after_discount = discount.apply(check_price)
if after_discount.gross <= 0: