Page 1 of 1

Session Based Shopping Cart

Posted: Sun May 02, 2010 10:14 am
by nitediver
Im going to build simple cart system(with list of chosen product, modifying product quantity).
Im wondering using session with array, is that the right way?

Any advice?

Thanks

Re: Session Based Shopping Cart

Posted: Sun May 02, 2010 10:36 am
by flying_circus
That should work just fine, just keep the data that you store in the array to a minimum and pretty simple. For example, an array of product id's, and on each page load, you'd build the cart by looking up the id's in the database.

Rather than using session's, data like that might be more appropriately stored in cookies. This will allow you to expire the session, but retain the user's shopping cart for a longer period of time.

Re: Session Based Shopping Cart

Posted: Mon May 03, 2010 4:49 am
by nitediver
I have been thinking of using cookies but since Im new in PHP, I would rather choose easier way, session.

What about changing the item quantity, process is done before storing data to database right?

Re: Session Based Shopping Cart

Posted: Mon May 03, 2010 1:15 pm
by flying_circus
Neither method is easier or more difficult than the other. Sessions are typically based on cookies.

This is how I believe shopping carts should operate, but this is more or less dependant on your design.

You will have 2 types of users, those who are registered with an account, and those who are not (guests). Generally sessions are cookie based and you would use sessions to track and authenticate users. You can store cart data in sessions, but sessions typically expire when you close your browser window. This means, if I add pork chops to my shopping cart and close the window, the next time I visit your site, my pork chops are gone from my cart because my session expired. However, if you store my cart in a cookie and set the lifetime of the cookie to something like 30 days, then my pork chops will still be in my cart on my next visit.

Its up to you, the major difference between cookies and session is where the data is stored.



Changing item quantity on what? On items in my cart? I thought you were storing the cart in a session, so you shouldnt need to update the database, just update the session data. If you mean changing item quantity to decrement your inventory on hand when I buy something, then a SQL transaction is probably your best bet.

Re: Session Based Shopping Cart

Posted: Wed May 05, 2010 7:42 am
by nitediver
What about storing item in cart into database, I want to make table to store all order, but item in cart is more than one, I dont know how to put multi value in one field.

This is my Order Table example:
[text]
order_id | order_user | order_item | order_qty
--------------------------------------------------------
001 | John | prod1, prod2 | 3
002 | Mike | prod3, prod4 | 5
-------------------------------------------------------
[/text]

That is all I can imagine, is that correct?

Re: Session Based Shopping Cart

Posted: Wed May 05, 2010 12:04 pm
by flying_circus
Ok, I think I see what you are after. Create 2 tables. The first table contains unique order details, such as the order number, customer name, address, etc. The second table will store the items for each order.

table 1:
- Order Id
- Customer Name
- Address

table 2:
- Order Id
- Item Id
- Item Quantity

The two tables are linked based on "Order Id", so that is how you determine which items belong with each order.

Re: Session Based Shopping Cart

Posted: Thu May 06, 2010 9:45 am
by nitediver
Yes, that's what I mean, I never thought of create two table for order, thanks.

Re: Session Based Shopping Cart

Posted: Thu May 06, 2010 9:58 am
by Apollo
I've done this once (for a pretty basic simple shop) by simply having one 'items' field in the order table, and storing values like 'productA,productB,productC' there. Or '2,productA,3,productX' meaning 2 units of productA and 3 units of productX.

SQL purists probably feel uncomfortable by this approach, but for my purpose in this situation it worked perfectly and was very simple and convenient to work with. Some implode and explode calls were easier in this case than combining results of several SQL queries.