Skip to content

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

Save discount in order

Michał Ociepka6d16cceed178
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index b511fdcdac..062a0ada88 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -8,8 +8,8 @@ from prices import Price, FixedDiscount
from ..cart import Cart
from ..core import analytics
+from ..discount.models import Voucher, NotApplicable
from ..order.models import Order
-from saleor.discount.models import Voucher, NotApplicable
from ..shipping.models import ShippingMethodCountry
from ..userprofile.models import Address, User
@@ -197,17 +197,29 @@ class Checkout(object):
self.shipping_address, is_shipping=True)
else:
shipping_address = None
- billing_address = self._save_address(self.billing_address, is_billing=True)
+ billing_address = self._save_address(
+ self.billing_address, is_billing=True)
+
+ order_data = {
+ 'billing_address': billing_address,
+ 'shipping_address': shipping_address,
+ 'tracking_client_id': self.tracking_code,
+ 'total': self.get_total()}
+
if self.user.is_authenticated():
- order = Order.objects.create(
- billing_address=billing_address, shipping_address=shipping_address,
- user=self.user, total=self.get_total(),
- tracking_client_id=self.tracking_code)
+ order_data['user'] = self.user
else:
- order = Order.objects.create(
- billing_address=billing_address, shipping_address=shipping_address,
- anonymous_user_email=self.email, total=self.get_total(),
- tracking_client_id=self.tracking_code)
+ # TODO: we should always save email in order not only for anonymous
+ order_data['anonymous_user_email'] = self.email
+
+ voucher = self._get_voucher()
+ if voucher is not None:
+ discount = self.discount
+ order_data['voucher'] = voucher
+ order_data['discount_amount'] = discount.amount
+ order_data['discount_name'] = discount.name
+
+ order = Order.objects.create(**order_data)
for partition in self.cart.partition():
shipping_required = partition.is_shipping_required()
@@ -223,22 +235,31 @@ class Checkout(object):
shipping_method_name=shipping_method_name)
group.add_items_from_partition(partition)
+ if voucher is not None:
+ voucher.used += 1
+ voucher.save(update_fields=['used'])
+
return order
- def recalculate_discount(self):
+ def _get_voucher(self):
voucher_code = self.voucher_code
if voucher_code is not None:
try:
- voucher = Voucher.objects.get(code=voucher_code)
+ return Voucher.objects.get(code=self.voucher_code)
except Voucher.DoesNotExist:
- del self.voucher_code
+ return None
+
+ def recalculate_discount(self):
+ voucher = self._get_voucher()
+ if voucher is not None:
+ try:
+ self.discount = voucher.get_discount_for_checkout(self)
+ except NotApplicable:
del self.discount
- else:
- try:
- self.discount = voucher.get_discount_for_checkout(self)
- except NotApplicable:
- del self.discount
- del self.voucher_code
+ del self.voucher_code
+ else:
+ del self.discount
+ del self.voucher_code
def get_subtotal(self):
zero = Price(0, currency=settings.DEFAULT_CURRENCY)
diff --git a/saleor/dashboard/templates/dashboard/order/detail.html b/saleor/dashboard/templates/dashboard/order/detail.html
index 276ededc77..64a1735e2e 100644
--- a/saleor/dashboard/templates/dashboard/order/detail.html
+++ b/saleor/dashboard/templates/dashboard/order/detail.html
@@ -48,6 +48,9 @@
<span class="card-title black-text">Order summary</span>
<ul>
+ {% if order.discount_amount %}
+ <li>{{ order.discount_name }}: <strong>-&nbsp;{% gross order.discount_amount %}</strong></li>
+ {% endif %}
<li class="text-lg">{% trans "Order total" %}: <strong>{% gross order.get_total %}</strong></li>
<li>
{% trans "Customer:" %}
diff --git a/saleor/discount/migrations/0003_auto_20160207_0534.py b/saleor/discount/migrations/0003_auto_20160207_0534.py
new file mode 100644
index 0000000000..b6db741bce
--- /dev/null
+++ b/saleor/discount/migrations/0003_auto_20160207_0534.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.1 on 2016-02-07 11:34
+from __future__ import unicode_literals
+
+import datetime
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('discount', '0002_voucher'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='sale',
+ name='type',
+ field=models.CharField(choices=[('fixed', 'USD'), ('percentage', '%')], default='fixed', max_length=10),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='code',
+ field=models.CharField(db_index=True, max_length=12, unique=True, verbose_name='code'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='discount_value',
+ field=models.DecimalField(decimal_places=2, max_digits=12, verbose_name='discount value'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='discount_value_type',
+ field=models.CharField(choices=[('fixed', 'USD'), ('percentage', '%')], default='fixed', max_length=10, verbose_name='discount type'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='end_date',
+ field=models.DateField(blank=True, null=True, verbose_name='end date'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='name',
+ field=models.CharField(blank=True, max_length=255, null=True, verbose_name='name'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='start_date',
+ field=models.DateField(default=datetime.date.today, verbose_name='start date'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='type',
+ field=models.CharField(choices=[('value', 'All purchases'), ('product', 'One product'), ('category', 'A category of products'), ('shipping', 'Shipping')], default='value', max_length=20, verbose_name='discount for'),
+ ),
+ migrations.AlterField(
+ model_name='voucher',
+ name='usage_limit',
+ field=models.PositiveIntegerField(blank=True, null=True, verbose_name='usage limit'),
+ ),
+ ]
diff --git a/saleor/order/migrations/0011_auto_20160207_0534.py b/saleor/order/migrations/0011_auto_20160207_0534.py
new file mode 100644
index 0000000000..98731aaf6c
--- /dev/null
+++ b/saleor/order/migrations/0011_auto_20160207_0534.py
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.1 on 2016-02-07 11:34
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+import django_prices.models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('discount', '0003_auto_20160207_0534'),
+ ('order', '0010_auto_20160119_0541'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='order',
+ name='discount_amount',
+ field=django_prices.models.PriceField(blank=True, currency='USD', decimal_places=2, max_digits=12, null=True),
+ ),
+ migrations.AddField(
+ model_name='order',
+ name='discount_name',
+ field=models.CharField(blank=True, default='', max_length=255),
+ ),
+ migrations.AddField(
+ model_name='order',
+ name='voucher',
+ field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='discount.Voucher'),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index a53090b8b0..889ca5a766 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -19,6 +19,7 @@ from prices import Price
from satchless.item import ItemLine, ItemSet
from ..core.utils import build_absolute_uri
+from ..discount.models import Voucher
from ..product.models import Product, ProductVariant
from ..userprofile.models import Address
@@ -64,6 +65,12 @@ class Order(models.Model, ItemSet):
pgettext_lazy('Order field', 'total'),
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
blank=True, null=True)
+ voucher = models.ForeignKey(
+ Voucher, null=True, related_name='+', on_delete=models.SET_NULL)
+ discount_amount = PriceField(
+ currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
+ blank=True, null=True)
+ discount_name = models.CharField(max_length=255, default='', blank=True)
class Meta:
ordering = ('-last_status_change',)
diff --git a/saleor/order/templates/order/payment.html b/saleor/order/templates/order/payment.html
index 9ebae642f4..a2718384c4 100644
--- a/saleor/order/templates/order/payment.html
+++ b/saleor/order/templates/order/payment.html
@@ -133,4 +133,24 @@
</table>
</div>
{% endfor %}
+ {% if order.discount_amount %}
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ {{ order.discount_name }}
+ </div>
+ <div class="panel-body text-right">
+ <strong>-&nbsp;{% gross order.discount_amount %}</strong>
+ </div>
+ </div>
+ {% endif %}
+ {% if order.discount_amount or groups|length > 1 %}
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ {% trans "Total" %}
+ </div>
+ <div class="panel-body text-right">
+ <strong>{% gross order.total %}</strong>
+ </div>
+ </div>
+ {% endif %}
{% endblock content %}