Anonymous user loses cart contents after login

Project: 
Ubercart
Category: 
bug report
Version: 
Ubercart 1.3
Priority: 
normal
Status: 
active

Know that you were working on this, but it's still happening. Shop, then cart, then login, then all gone!

Re: Anonymous user loses cart contents after login

Actually... I was unaware of any remaining issues. Are you running the latest Drupal/Ubercart? There was an issue in the updates from Drupal 5.8-5.9 that affected this, but it should've been fixed in UC 1.3.

Re: Re: Anonymous user loses cart contents after login

I just like to mention that I am experiencing the same. Right now I am testing Übercart 1.3 with Drupal 5.9 and ... same here. As an anonymous user you will loose your cart content when logging in. Maybe you could give me a short explanation why this happens?

Re: Re: Re: Anonymous user loses cart contents after login

This is working for me on the Livetest. Please try it out there and let me know if you can reproduce the bug. It may be that your last update didn't overwrite uc_cart.module or something... that happens to folks quite often.

Re: Re: Re: Re: Anonymous user loses cart contents after login

Strange. My setup is the following: Drupal 5.10 (lastest maintenance release), UC 1.3 RC1. Anonymous checkout disabled. Administrators have to approve new user accounts first. The senario is the following: As an anonymous user, I put something in the shopping cart and click "checkout". I register for a new account. The next page that says "Thank you for applying for an account. Your account is currently pending ..." says also "There are no products in your shopping cart...". Next scenario: as an anonymous user I put something in my cart, click "Checkout", login as an registered user, next page says "Your shopping cart is empty".

I came up with the idea that this behaviour might have something to do with session handling. I got the following information from the Devel module:

As anonymous user, when I have put something in my cart, session viewer says:

Array
(
[checkout-redirect] => 1
)
Session name: SSESSb90b8454830cdc5abd05c6070513d1bf
Session ID: dgemdn7772ekvhsvmamcshunv7

After checking in:
Session name: SESSb90b8454830cdc5abd05c6070513d1bf
Session ID: rb8aiffvi54epdbmhmfufs29j5

As you can see, the session ID has been changed after loggin in. Is that the proper behaviour? You mentioned something about an issue with Drupal 5.8 / 5.9. Could you give me some more details. Maybe I will able to solve this out by myself.

Thanks!

ps. you've done a great job with UC! I am still a newbie when it comes to Drupal / UC, but I have been coding PHP for production sites for about 5 years now. ds.

Re: Re: Re: Re: Re: Anonymous user loses cart contents after log

Hmm... you know, I think I actually ran into this with a client, and the recourse at the time was to simply disable account validation. I tend to think it should work without having to resort to that, but I'd honestly recommend it for conversion purposes anyways (unless you just really really need account moderation). Also, it may be a bug that got massaged out in the actual 1.3 release (the one after RC1).

Thanks for your kind words. Smiling

EDIT: Looking at the source, the offending function is uc_cart_user() in uc_cart.module. I do believe you're right about the session rebuilding... if the user was to login at the time they created the account, the cart should be updated no problem. Honestly, I think a good solution moving forward would be to not use the session ID but actually have a cart ID session variable. Seems like a no brainer. Sticking out tongue

Re: Re: Re: Re: Re: Re: Anonymous user loses cart contents after

After digging into the code a little bit deeper, I now think I am on to something. I think that this issue has nothing to do with UC, rather with the fact how Drupal handels user sessions. Especially session handling when a anonymous user loggs in (and viceversa). I think that this thread (http://drupal.org/node/280934) might have something to do with it. Commenting out 'session_regenerate_id () form the following code actually overcomes the issue:

-- from session.inc in the Drupal core include files ---

/**
* Called when an anonymous user becomes authenticated or vice-versa.
*/
function sess_regenerate() {
$old_session_id = session_id();

// We code around http://bugs.php.net/bug.php?id=32802 by destroying
// the session cookie by setting expiration in the past (a negative
// value). This issue only arises in PHP versions before 4.4.0,
// regardless of the Drupal configuration.
// TODO: remove this when we require at least PHP 4.4.0
if (isset($_COOKIE[session_name()])) {
//setcookie(session_name(), '', time() - 42000, '/');
}

//session_regenerate_id();

//db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
}

But, I am not quite sure yet how this affects other modules or Drupal core functions.

"PHP 5.2 or higher is only supported by Drupal 4.7.5 or higher"

Bug fixed in 2005...

Re: Re: Re: Re: Re: Re: Re: Anonymous user loses cart contents a

You shouldn't comment out session_regenerate_id because you'll risk session fixation attack against your site.

The problem easily can be circumvented by using your own session variable to store the cart id. The permanent session id is not something you can rely on and you shouldn't have.

patch for the issue

I posted a patch fixing this at drupal.org. Please review it.

Odly, even though this

Odly, even though this ticket on d.o is closed i am still having this problem in drupal 6 with Ubercart 6.x.2.0-RC3, an anon user is prompted to "click here to login with an existing account and return to checkout". and when they do, their cart which previously had some number of products in it, is empty after login.

Similar problem...

I submitted this on another thread, but thought that posting it here might be useful also:

I have a similar problem, but in my case if the anon user adds items to their cart before logging in and then decides to login from the checkout page, their cart is updated with the same items again. I am using the Discount module, so after they login our 'member discounts' are applied. Wondering if this is the cause?
Running...
[php_version] => 5.2.9
[drupal_version] => 5.5
[db_type] => mysql
[db_version] => 4.0.27
[ubercart_version] => 5.x-1.5

UPDATE:
It seems that the only time this occurs is when I login as admin. I have a member account also, and when I login using that account, I am brought back to the checkout and the correct items are there, no duplication. Perhaps it's because as admin I have two Roles assigned...Member and Site Editor?

Re: Odly, even though this

we fixed this on our site. It seems that it may have been related to our custom module that wrote some but not all data from the order into a uc node checkout node.