Search code examples
sessiondrupal-7ubercart

How to persist cart in UberCart?


I have a cart to which I add Products A and B. Product A can be bought without B, but if you buy Product A, you must buy Product B as well. I wrote a simple table to keep track of these relationships and stops you from checking out when they rules are broken.

My problem comes in when a user add to their cart and then proceeds to register. After registration the cart_item_id has changed and the user's session_id has changed.

How can I match up the session from the anonymous user with the session of the now registered and logged in user?


Solution

  • Instead of using a second table for the data I wanted to persist, I realised I could just add any data to the cartContents and it will automatically keep all the information i store when the user logs in and out.

    This is how I did the update:

    $cartItemID = 1; // the id of the item in the cart I want to add info to
    $cartContents = uc_cart_get_contents();
    foreach ($cartContents as $c) 
    {
        if ($c->cart_item_id == $cartItemID)
        {
            $c->data['my_chosen_key'] = $sessionData;
            uc_cart_update_item($c);
        }
    }
    

    Information stays if you log out and back in.