简体   繁体   中英

Connecting a users shopping basket to their user account? PHP

I'm confused on how to do this. Say the user creates an account with my ecommerce website, and then starts adding products into their basket. If I store the users username and password in a database table, and use sessions/cookies to manage the products in their shopping basket, what would I need to do in order to connect the users shopping basket to their account, so that when they log in they will be able to see the items they had previously stored?

Do I first allow the user to login, query whether they logged in successfully, and then make a session/cookie variable for their username? Or do I have to store the users cart items in a database and connect it with the user accounts table?

I'm confused with how to store a shopping baskets items into a table. Is that even the right way to do it?

There's no code yet, I want to create the databases correctly before I start coding and just need some advice. Thank you

If you have two tables, one for the users and one for the items, you can do something like the following.

Manage current basket items by adding to and removing the item_ids into a serialized array, which you can store in your users table AND the session at the same time, keeping them in sync. For example, if a user visits your store for the first time (not logged in, and empty shopping basket), you can create the session like so.

session_start();

We start the session.

if (isset($_SESSION['current_basket']) {
    $current_basket = unserialize($_SESSION['current_basket']);
} else {
    $current_basket = array();
}

Because this is the first time our visitor has visited our page, the session variable current_basket will not be set, meaning the above if statement will not run and instead just create an empty PHP array in a variable called current_basket .

Now, when the visitor adds an item to the basket, we just need the item's ID in your database and add it into the current_basket array.

$current_basket[] = $item_id;

Then we immediately update the session variable with the new array, serializing it in the process.

$_SESSION['current_basket'] = serialize($current_basket);

You now have a proper, usable array with all the product IDs for that person's shopping basket.

Now, let's pretend the user was logged in. We'd check for that and only add one more step.

$sql = "UPDATE users SET current_basket=" . serialize($current_basket) . " WHERE id=$user_id"
// Execute that query.

There, now that same array is in the database which you can then pull and set as a session variable when the user logs in.

You can run these series of steps everytime a product is added or deleted, keeping it updated both in the session and the database.

This is obvisouly a very watered down concept for the sake of explaining. Obviously, you'd need to manage the array better. For example, not adding a product to the basket if it's already there, removing items from the array...etc.

The key is to serialize the array, and store in the session always, and in the users table if the user is logged in.

Then when the user comes back and logs in, you can set the array in the session for them using the array in the database from when they were last on your site.

I would have the cart table cart_id / user_id / date_time_created then an item table indexed on the cart_id.

Also add a way to dump the cart_id and items out of the tables after so many hours or days otherwise your database will get huge.

And let your end users know that the items will be dumped after x amount of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM