Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve checkout experience
Patryk Zawadzki8840275c69bb
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex 38548ec61e..eaf6769576 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -4,7 +4,7 @@ from functools import wrapsfrom django.conf import settingsfrom django.db import transactionfrom django.forms.models import model_to_dict-from prices import Price+from prices import FixedDiscount, Pricefrom ..cart import Cartfrom ..core import analytics@@ -179,14 +179,35 @@ class Checkout(object):return order+ def get_subtotal(self):+ zero = Price(0, currency=settings.DEFAULT_CURRENCY)+ cost_iterator = (+ shipment.get_total()+ for shipment, shipping_cost, total in self.deliveries)+ total = sum(cost_iterator, zero)+ return total+def get_total(self):zero = Price(0, currency=settings.DEFAULT_CURRENCY)cost_iterator = (- total_with_shipping for shipping, shipping_cost, total_with_shipping- in self.deliveries)+ total+ for shipment, shipping_cost, total in self.deliveries)total = sum(cost_iterator, zero)return total+ def get_total_shipping(self):+ zero = Price(0, currency=settings.DEFAULT_CURRENCY)+ cost_iterator = (+ shipping_cost+ for shipment, shipping_cost, total in self.deliveries)+ 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)diff --git a/saleor/checkout/templates/checkout/details.html b/saleor/checkout/templates/checkout/details.htmlindex 9aa670458b..f030a7c739 100644--- a/saleor/checkout/templates/checkout/details.html+++ b/saleor/checkout/templates/checkout/details.html@@ -1,8 +1,7 @@{% extends "base.html" %}{% load i18n %}-{% load gross from prices_i18n %}-{% load tax from prices_i18n %}+{% load gross net tax from prices_i18n %}{% load bootstrap_form bootstrap_button from bootstrap3 %}@@ -63,100 +62,89 @@</div><div class="col-sm-4">- {% if checkout.is_shipping_required and checkout.shipping_address.country %}- <div class="panel panel-default">- <div class="panel-heading">{% trans "Shipping address" %}</div>- <div class="panel-body">- {% include "userprofile/snippets/address-details.html" with address=checkout.shipping_address only %}- </div>- </div>- {% endif %}--- {% for delivery, delivery_cost, total_with_delivery in checkout.deliveries %}- <div class="panel panel-default">- <div class="panel-heading">- {% if delivery.is_shipping_required %}- {% blocktrans with number=forloop.counter %}Shipment #{{ number }}{% endblocktrans %}- {% else %}- {% blocktrans with number=forloop.counter %}Digital delivery #{{ number }}{% endblocktrans %}- {% endif %}- </div>- <table class="table">- <thead>+ <table class="table">+ <tbody>+ {% for delivery, delivery_cost, total_with_delivery in checkout.deliveries %}+ {% for item in delivery %}<tr>- <th class="col-sm-6">{% trans "Item" %}</th>- <th class="col-sm-3 text-right">{% trans "Price" %}</th>- <th class="col-sm-3 text-right">{% trans "Subtotal" %}</th>+ <td>{{ item.product }}{% if item.quantity != 1 %} <span class="badge">×{{ item.quantity }}</span>{% endif %}</td>+ <td class="text-right">{% net item.get_total %}</td></tr>- </thead>- <tfoot>- <tr>- <th colspan="2">{% trans "Total" %}</th>- <th class="text-right">{% gross total_with_delivery %}</th>- </tr>- </tfoot>- <tbody>- {% for item in delivery %}- <tr>- <td>{{ item.product }}{% if item.quantity != 1 %} <em>×{{ item.quantity }}</em>{% endif %}</td>- <td class="text-right">{% gross item.get_price_per_item %}</td>- <td class="text-right">{% gross item.get_total %}</td>- </tr>- {% endfor %}- {% if delivery_cost.gross %}- <tr>+ {% endfor %}+ {% endfor %}++ {% if voucher_form %}+ <tr>+ {% if not voucher %}+ {% with discount=checkout.get_discount %}<td>- {% trans "Shipping" %}+ <form action="{% url "checkout:add-voucher" %}?next={{ request.get_full_path }}"+ method="post" class="form-inline">+ {% csrf_token %}+ <span class="text-success">{{ discount.name }}</span>+ <button type="submit" class="btn btn-link" aria-label="{% trans "Remove" %}"><span class="glyphicon glyphicon-remove-circle"></span></button>+ </form></td>- <td class="text-right">{% gross delivery_cost %}</td>- <td class="text-right">{% gross delivery_cost %}</td>- </tr>+ <td class="text-right">+ {% net discount.amount %}+ </td>+ {% endwith %}+ {% else %}+ <td colspan="2">+ <form action="{% url "checkout:add-voucher" %}?next={{ request.get_full_path }}"+ method="post" class="form-inline text-center">+ {% csrf_token %}+ {% bootstrap_form voucher_form layout="inline" %}+ {% bootstrap_button _("Apply") button_type="submit" button_class="btn-primary" %}+ </div>+ </form>+ </td>{% endif %}- </tbody>- </table>- </div>- {% endfor %}-- {% if voucher_form %}- {% if voucher %}- <form action="{% url "checkout:remove-voucher" %}?next={{ request.get_full_path }}"- method="post" class="form">- {% csrf_token %}- <div class="row">- <div class="col-sm-8">- <strong>{{ voucher }}</strong>- </div>- <div class="col-sm-4">- {% bootstrap_button _("Remove") button_type="submit" button_class="btn-block btn-danger" %}- </div>- </div>- </form>- {% else %}- <form action="{% url "checkout:add-voucher" %}?next={{ request.get_full_path }}"- method="post" class="form">- {% csrf_token %}- <div class="row">- <div class="col-sm-8">- {% bootstrap_form voucher_form show_label=False %}- </div>- <div class="col-sm-4">- {% bootstrap_button _("Apply") button_type="submit" button_class="btn-block btn-default" %}- </div>- </div>- </form>- {% endif %}- {% endif %}-- {% with total=checkout.get_total %}- <div class="alert alert-success">- {% trans "Your total will be" %}:- <strong>{% gross total %}</strong>- {% if total.tax %}- ({% blocktrans with tax=total.tax|floatformat:2 currency=total.currency %}includes {{ tax }} <span class="currency">{{ currency }}</span> tax{% endblocktrans %})+ </tr>{% endif %}- </div>- {% endwith %}++ {% with total=checkout.get_total %}+ <tr>+ <td>+ {% trans "Subtotal" %}+ </td>+ <td class="text-right">+ {% net checkout.get_subtotal %}+ </td>+ </tr>+ <tr>+ <td>+ {% trans "Shipping" %}+ </td>+ <td class="text-right">+ {% if checkout.shipping_method %}+ {% net checkout.get_total_shipping %}+ {% else %}+ —+ {% endif %}+ </td>+ </tr>+ {% if total.tax or True %}+ <tr>+ <td>+ {% trans "Taxes" %}+ </td>+ <td class="text-right">+ {% tax total %}+ </td>+ </tr>+ {% endif %}+ <tr>+ <th>+ {% trans "Total" %}+ </th>+ <td class="text-right">+ <strong>{% gross total %}</strong>+ </td>+ </tr>+ {% endwith %}+ </tbody>+ </table></div></div>{% endblock content %}diff --git a/saleor/checkout/templates/checkout/shipping_method.html b/saleor/checkout/templates/checkout/shipping_method.htmlindex 50e2058082..2ec6c5c48a 100644--- a/saleor/checkout/templates/checkout/shipping_method.html+++ b/saleor/checkout/templates/checkout/shipping_method.html@@ -32,6 +32,10 @@{% block forms %}+ <h3>{% trans "Shipping address" %}</h3>++ {% include "userprofile/snippets/address-details.html" with address=checkout.shipping_address only %}+<form method="post" novalidate>{% csrf_token %}<h3>{% trans "Shipping method" %}</h3>diff --git a/saleor/checkout/views/discount.py b/saleor/checkout/views/discount.pyindex 4337db0d80..f6cc8afac4 100644--- a/saleor/checkout/views/discount.py+++ b/saleor/checkout/views/discount.py@@ -1,5 +1,7 @@from functools import wraps+from django.shortcuts import redirect+from django.template.response import TemplateResponsefrom ...discount.forms import GetVoucherForm@@ -11,10 +13,11 @@ def add_voucher_form(view):initial = {'voucher': voucher}voucher_form = GetVoucherForm(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+ response = view(request, checkout)+ if isinstance(response, TemplateResponse):+ response.context_data['voucher_form'] = voucher_form+ response.context_data['voucher'] = voucher+ return responsereturn funcdiff --git a/saleor/checkout/views/shipping.py b/saleor/checkout/views/shipping.pyindex 8eb408c118..290c7b0f75 100644--- a/saleor/checkout/views/shipping.py+++ b/saleor/checkout/views/shipping.py@@ -1,8 +1,9 @@from django.shortcuts import redirectfrom django.template.response import TemplateResponse-from saleor.checkout.forms import AnonymousUserShippingForm, ShippingAddressesForm-from saleor.userprofile.forms import AddressForm-from saleor.userprofile.models import Address++from ..forms import AnonymousUserShippingForm, ShippingAddressesForm+from ...userprofile.forms import AddressForm+from ...userprofile.models import Addressdef anonymous_user_shipping_address_view(request, checkout):