Skip to content

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

Add anonymous user email into billing step

Michal Ociepka48cf00cc8864
diff --git a/checkout/forms.py b/checkout/forms.py
index d8f2a37324..9a4639425e 100644
--- a/checkout/forms.py
+++ b/checkout/forms.py
@@ -62,7 +62,7 @@ class DeliveryField(forms.ChoiceField):
return self.methods[int(value)]
except (IndexError, ValueError):
raise ValidationError(
- self.error_messages['invalid_choice'] % {'value':value})
+ self.error_messages['invalid_choice'] % {'value': value})
def valid_value(self, value):
return value in self.methods
@@ -73,3 +73,8 @@ class DeliveryForm(forms.Form):
def __init__(self, delivery_methods, *args, **kwargs):
super(DeliveryForm, self).__init__(*args, **kwargs)
self.fields['method'] = DeliveryField(delivery_methods)
+
+
+class AnonymousEmailForm(forms.Form):
+
+ email = forms.EmailField()
diff --git a/checkout/steps.py b/checkout/steps.py
index 8b6d1ebd0c..557b55e72a 100644
--- a/checkout/steps.py
+++ b/checkout/steps.py
@@ -1,4 +1,5 @@
from .forms import ManagementForm, DigitalDeliveryForm, DeliveryForm
+from checkout.forms import AnonymousEmailForm
from django.core.exceptions import ValidationError
from django.db import models
from django.shortcuts import redirect
@@ -67,9 +68,17 @@ class BaseShippingStep(BaseCheckoutStep):
class BillingAddressStep(BaseShippingStep):
+ template = 'checkout/billing.html'
+ anonymous_user_email = None
+
def __init__(self, checkout, request):
address = checkout.billing_address or Address()
super(BillingAddressStep, self).__init__(checkout, request, address)
+ if not request.user.is_authenticated():
+ self.anonymous_user_email = self.checkout.anonymous_user_email
+ initial = {'email': self.anonymous_user_email}
+ self.forms['anonymous'] = AnonymousEmailForm(request.POST or None,
+ initial=initial)
def __str__(self):
return 'billing-address'
@@ -77,15 +86,32 @@ class BillingAddressStep(BaseShippingStep):
def __unicode__(self):
return u'Billing Address'
+ def forms_are_valid(self):
+ forms_are_valid = super(BillingAddressStep, self).forms_are_valid()
+ if 'anonymous' not in self.forms:
+ return forms_are_valid
+ anonymous_form = self.forms['anonymous']
+ if forms_are_valid and anonymous_form.is_valid():
+ self.anonymous_user_email = anonymous_form.cleaned_data['email']
+ return True
+ return False
+
def save(self):
+ self.checkout.anonymous_user_email = self.anonymous_user_email
self.checkout.billing_address = self.address
self.checkout.save()
def add_to_order(self, order):
self.address.save()
+ order.anonymous_user_email = self.anonymous_user_email
order.billing_address = self.address
order.save()
+ def validate(self):
+ super(BillingAddressStep, self).validate()
+ if 'anonymous' in self.forms and not self.anonymous_user_email:
+ raise InvalidData()
+
class ShippingStep(BaseShippingStep):
diff --git a/checkout/templates/checkout/address.html b/checkout/templates/checkout/address.html
index 1846d05553..ded9d0b165 100644
--- a/checkout/templates/checkout/address.html
+++ b/checkout/templates/checkout/address.html
@@ -5,6 +5,9 @@
{% block forms %}
<form method="post" class="form-horizontal">
{% csrf_token %}
+
+ {% block extra_forms %}{% endblock %}
+
{{ step.forms.management|as_bootstrap }}
{% if user.is_authenticated %}
<fieldset>
@@ -16,7 +19,6 @@
<legend>Complete address:</legend>
{{ step.forms.address|as_bootstrap }}
</fieldset>
- {% block extra_forms %}{% endblock %}
<div class="form-actions">
<button type="submit" class="btn btn-primary">
diff --git a/checkout/templates/checkout/billing.html b/checkout/templates/checkout/billing.html
new file mode 100644
index 0000000000..e55459c20b
--- /dev/null
+++ b/checkout/templates/checkout/billing.html
@@ -0,0 +1,14 @@
+{% extends "checkout/address.html" %}
+
+{% load as_bootstrap from bootstrap %}
+
+{% block extra_forms %}
+{% if not user.is_authenticated %}
+<fieldset>
+ <legend>
+ User data:
+ </legend>
+ {{ step.forms.anonymous|as_bootstrap }}
+</fieldset>
+{% endif %}
+{% endblock extra_forms %}