'Django: Eccomerce website. When I proceed the checkout it's saving the order if person is not logged in, but when it's not AnonymousUser error
I am working on ecommerce website and it's time to make checkout. In my website, when to order product without an account it's saving the order, but if it's with an account, it's going to another page, but not erasing a cart and isn't saving the order. What is the problem? Can you please help me to solve this problem. It's not working only when client is logged in an account. views.py
def processOrder(request):
transaction_id = datetime.datetime.now().timestamp()
data = json.loads(request.body)
tel = data['shipping']['number'],
address=data['shipping']['address'],
city=data['shipping']['city'],
state=data['shipping']['state'],
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
else:
customer, order = guestOrder(request, data)
total = float(data['form']['total'])
order.transaction_id = transaction_id
order.tel= tel
order.address=address
order.city=city
order.state=state
if total == order.get_cart_total:
order.complete = False
order.save()
return JsonResponse('Payment submitted..', safe=False)
html
<form class="form" method="POST" action="#" id="form">
<div class="row">
<div class="col-lg-6 col-md-6 col-12" id="user-info">
<div class="form-group">
<label>Имя<span>*</span></label>
<div>
<input type="text" name="name" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="user-info">
<div class="form-group">
<label>Фамилия<span>*</span></label><div>
<input type="text" name="surname" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="user-info">
<div class="form-group">
<label>Email<span>*</span></label><div>
<input type="email" name="email" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="user-info shipping-info">
<div class="form-group">
<label>Номер телефона<span>*</span></label><div>
<input type="number" name="number" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="shipping-info">
<div class="form-group">
<label>Город<span>*</span></label><div>
<input type="text" name="city" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="shipping-info">
<div class="form-group">
<label>State<span>*</span></label><div>
<input type="text" name="state" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="shipping-info">
<div class="form-group">
<label>Адрес<span>*</span></label><div>
<input type="text" name="address" placeholder="" required="required"></div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-12" id="shipping-info">
<div class="form-group">
<label>Адрес-2<span>*</span></label><div>
<input type="text" name="address1" placeholder="" required="required"></div>
</div>
</div>
</div>
<div class="button">
<a href="#" class="btn" id="make-payment">proceed to checkout</a>
</div>
script fetch
function getToken(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getToken('csrftoken')
var total = '{{order.get_cart_total}}'
var form = document.getElementById('form')
form.addEventListener('submit', function(e){
e.preventDefault()
console.log('Form Submitted...')
})
document.getElementById('make-payment').addEventListener('click', function(e){
submitFormData()
})
function submitFormData(){
console.log('Payment button clicked')
var userFormData = {
'name':null,
'surname':null,
'email':null,
'total':total,
}
var shippingInfo = {
'name':null,
'surname':null,
'email':null,
'number':null,
'address':null,
'city':null,
'state':null,
}
shippingInfo.address = form.address.value
shippingInfo.city = form.city.value
shippingInfo.state = form.state.value
shippingInfo.number = form.number.value
userFormData.name = form.name.value
userFormData.surname = form.surname.value
userFormData.email = form.email.value
console.log('Shipping Info:', shippingInfo)
console.log('User Info:', userFormData)
var url = "/process_order/"
fetch(url, {
method:'POST',
headers:{
'Content-Type':'applicaiton/json',
'X-CSRFToken':csrftoken,
},
body:JSON.stringify({'form':userFormData, 'shipping':shippingInfo}),
})
.then((data) => {
console.log('Success:', data);
alert('Transaction completed');
cart = {}
document.cookie ='cart=' + JSON.stringify(cart) + ";domain=;path=/"
window.location.href = "{% url 'store' %}"
})
}
</script>
Please, help! Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
