Shopping Cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Shopping Cart

Post by phpcoder »

Hi,

I am using session variables for storing items for my shopping cart. How should I use session vars to store the Item ID and its Quantity.

Thanks
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

What have you got set up so far?

Show a little code to give us something to work with.

Cheers
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

Stryks wrote:What have you got set up so far?

Show a little code to give us something to work with.

Cheers
I am looking for a way to store in user session what ever user buy. I need to keep Item ID and QTY in user session so when the user check out I can bill the user properly and can send th eappropretaite qunatity to the user.
Thanks
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well .... I'd probably just create an array of the data to be carried and assign it to the session.

Code: Select all

$_SESSION['CART'] = array();
$item = array('P_ID'=>120, 'title'=>'Blue Widget', 'qty'=1, 'unit_cost'=>55);
$_SESSION['CART'][] = $item;
You can build the $item array from a database call when a user clicks the item's 'buy now' button and add it to the session as above.

If you know how arrays work, then you'll know that you now have an item saved to $_SESSION['CART'][0]. Adding another item in the same way will give you [1], then [2], etc etc. allowing you to have multiples of multiple items.

Then you could display cart contents with

Code: Select all

foreach($_SESSION['CART'] as $cart_id=>$cart_item) {
   echo "Item $cart_id - (item {$cart_item['P_ID']}){$cart_item['qty']} x {$cart_item['title']} @ ${$cart_item['unit_cost']} each <br />";
}
With access to the slot_id you can do things like increase a slots quantity if the user clicks the appropriate button.

Code: Select all

$_SESSION['CART'][0]['qty'] += 1;
To be really neat about it though, and to save your fingers from typing $_SESSION all over the place, you could build all of this into a class. If nothing else you'll prevent yourself getting freaked out thinking about how much you're tying yourself up with values in your sessions.

Hope this helps. 8)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

I would use database as almost always you want to have data to analyze... to which step a customer went and ditch the order, etc.
More data kept gives much more flexibility....
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Either way, the principal is pretty much the same.

Instead of accessing the session for your cart data, you'd access the database instead.

The suggested class implementation would be a champ for this too. You would just have it load the cart data when the class is instantiated, and update on change.

Have a tinker anyhow, and post back if you need some help with code.
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

Stryks wrote:Well .... I'd probably just create an array of the data to be carried and assign it to the session.

Code: Select all

$_SESSION['CART'] = array();
$item = array('P_ID'=>120, 'title'=>'Blue Widget', 'qty'=1, 'unit_cost'=>55);
$_SESSION['CART'][] = $item;
You can build the $item array from a database call when a user clicks the item's 'buy now' button and add it to the session as above.

If you know how arrays work, then you'll know that you now have an item saved to $_SESSION['CART'][0]. Adding another item in the same way will give you [1], then [2], etc etc. allowing you to have multiples of multiple items.

Then you could display cart contents with

Code: Select all

foreach($_SESSION['CART'] as $cart_id=>$cart_item) {
   echo "Item $cart_id - (item {$cart_item['P_ID']}){$cart_item['qty']} x {$cart_item['title']} @ ${$cart_item['unit_cost']} each <br />";
}
With access to the slot_id you can do things like increase a slots quantity if the user clicks the appropriate button.

Code: Select all

$_SESSION['CART'][0]['qty'] += 1;
To be really neat about it though, and to save your fingers from typing $_SESSION all over the place, you could build all of this into a class. If nothing else you'll prevent yourself getting freaked out thinking about how much you're tying yourself up with values in your sessions.

Hope this helps. 8)
Sorted . Thanks so much
Post Reply