awesto / django-shop

A Django based shop system
http://www.django-shop.org
BSD 3-Clause "New" or "Revised" License
3.17k stars 1.04k forks source link

GuestCustomer no checkout #805

Closed markusmo closed 4 years ago

markusmo commented 4 years ago

Hi I have an issue, and I am not quite sure, why it occurs.

When I am shopping as an anoymous customer, a visiting customer is created there is also a cart and cart_items visible in the database. Only in the UI there is a empty cart. I do not get any exceptions or something, which is quite strange. I checked all my foreign keys and my configuration, but I cannot see anything wrong there.

jrief commented 4 years ago

This is a feature. The cart is created together with the Session-Cookie. The Session-Cookie is created on first interaction with the cart. Reason is, that django-SHOP wants to allow a user to shop as guest. This means that we must create a cart tight to a user – an anonymous user. In Django however, anonymous users are indistinguishable. Therefore django-SHOP, creates pseudo anonymous users, which are tight to their session-ID.

You should check when your Django setup creates the session-ID. It normally happens when the first item is added to the cart.

markusmo commented 4 years ago

I checked. I do have a session-ID in my cookie, but when debugging, I do not have a customer in my request (for example in my cart-modifier). Should I switch to my own context processor, overwriting yours?

jrief commented 4 years ago

Did you add 'shop.middleware.CustomerMiddleware' to MIDDLEWARE in your settings? Since process_request is called on each request, the customer will for sure be added to the request object. Please check line 29.

markusmo commented 4 years ago

I added the middleware and I have a customer object in my settings. I can see a customer added to the request. But when adding a product to my cart and I am not logged in I can see the following error message in my cart modifier when looking at the customer owning the cart:

'Traceback (most recent call last): ... shop.models.defaults.cart.Cart.customer.RelatedObjectDoesNotExist: Cart has no customer.'

An the request in my cart-modifier does not have a customer ...

jrief commented 4 years ago

In model Cart, the foreign key customer is a non-nullable field. May it be, that due to a previous misconfiguration, the table myshop_cart became inconsistent? Please look at that table and delete all rows where customer_id points to nowhere.

markusmo commented 4 years ago

I had a look at it, and there are no carts with no customer

cart_entries
markusmo commented 4 years ago

Latest update works perfectly! Thanks