Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add discount form to checkout
Michał Ociepka7e524cbc84b7
diff --git a/saleor/checkout/templates/checkout/details.html b/saleor/checkout/templates/checkout/details.htmlindex 62306da067..6aff0a64ea 100644--- a/saleor/checkout/templates/checkout/details.html+++ b/saleor/checkout/templates/checkout/details.html@@ -3,6 +3,7 @@{% load i18n %}{% load gross from prices_i18n %}{% load tax from prices_i18n %}+{% load bootstrap_form bootstrap_button from bootstrap3 %}{% block title %}{% trans "Checkout" %} — {{ block.super }}{% endblock %}@@ -117,6 +118,24 @@</div>{% endfor %}+ {% if voucher_form %}+ <div class="panel panel-default">+ <div class="panel-heading">{% trans "Discount" %}</div>+ <div class="panel-body">+ {% if voucher %}+ {{ voucher }}+ {% else %}+ <form action="{% url "checkout:add-voucher" %}?next={{ request.get_full_path }}"+ method="post" class="form-inline">+ {% csrf_token %}+ {% bootstrap_form voucher_form layout='inline' %}+ {% bootstrap_button _("Apply") button_type="submit" button_class="btn-default" %}+ </form>+ {% endif %}+ </div>+ </div>+ {% endif %}+{% with total=checkout.get_total %}<div class="alert alert-success">{% trans "Your total will be" %}:diff --git a/saleor/checkout/urls.py b/saleor/checkout/urls.pyindex 8bc8070eaf..c76f8c086e 100644--- a/saleor/checkout/urls.py+++ b/saleor/checkout/urls.py@@ -1,8 +1,11 @@from django.conf.urls import url+from django.views.decorators.http import require_POSTfrom .core import load_checkoutfrom . import views+voucher_view = require_POST(views.discount.apply_voucher_view)+urlpatterns = [url(r'^$', load_checkout(views.index_view), name='index'),@@ -10,5 +13,6 @@ urlpatterns = [name='shipping-address'),url(r'^shipping-method/', load_checkout(views.shipping_method_view),name='shipping-method'),- url(r'^summary/', load_checkout(views.summary_view), name='summary')+ url(r'^summary/', load_checkout(views.summary_view), name='summary'),+ url(r'^add_voucher/', load_checkout(voucher_view), name='add-voucher')]diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.pyindex d7e25aef30..5d791e6da6 100644--- a/saleor/checkout/views/__init__.py+++ b/saleor/checkout/views/__init__.py@@ -2,6 +2,7 @@ from django.shortcuts import redirectfrom django.template.response import TemplateResponsefrom ..forms import ShippingMethodForm+from .discount import add_voucher_formfrom .validators import (validate_cart, validate_shipping_address,validate_shipping_method, validate_is_shipping_required)@@ -18,6 +19,7 @@ def index_view(request, checkout):@validate_cart@validate_is_shipping_required+@add_voucher_formdef shipping_address_view(request, checkout):if request.user.is_authenticated():return user_shipping_address_view(request, checkout)@@ -27,6 +29,7 @@ def shipping_address_view(request, checkout):@validate_cart@validate_is_shipping_required@validate_shipping_address+@add_voucher_formdef shipping_method_view(request, checkout):country_code = checkout.shipping_address.country.codeshipping_method_form = ShippingMethodForm(@@ -39,6 +42,7 @@ def shipping_method_view(request, checkout):@validate_cart+@add_voucher_formdef summary_view(request, checkout):if checkout.is_shipping_required:view = validate_shipping_address(summary_with_shipping_view)diff --git a/saleor/checkout/views/discount.py b/saleor/checkout/views/discount.pynew file mode 100644index 0000000000..05f76dfd52--- /dev/null+++ b/saleor/checkout/views/discount.py@@ -0,0 +1,27 @@+from functools import wraps+from django.shortcuts import redirect++from ...discount.forms import GetVoucherForm+++def add_voucher_form(view):+ @wraps(view)+ def func(request, checkout):+ voucher = None+ initial = {'voucher': voucher}+ voucher_form = GetVoucherForm(+ request.POST or None, prefix='discount', initial=initial)+ template_response = view(request, checkout)+ template_response.context_data['voucher_form'] = voucher_form+ template_response.context_data['voucher'] = voucher+ return template_response+ return func+++def apply_voucher_view(request, checkout):+ voucher_form = GetVoucherForm(request.POST or None, prefix='discount')+ next_url = request.GET.get('next', request.META['HTTP_REFERER'])+ if voucher_form.is_valid():+ voucher = voucher_form.cleaned_data['voucher']+ return redirect(next_url)+diff --git a/saleor/discount/forms.py b/saleor/discount/forms.pynew file mode 100644index 0000000000..798ac87dc3--- /dev/null+++ b/saleor/discount/forms.py@@ -0,0 +1,13 @@+from django import forms+from django.utils.translation import pgettext_lazy++from .models import Voucher+++class GetVoucherForm(forms.Form):++ voucher = forms.ModelChoiceField(+ queryset=Voucher.objects.all(), to_field_name='code',+ label=pgettext_lazy('voucher_form', 'code'),+ widget=forms.TextInput)+