Page 1 of 1

Shopping Cart

Posted: Mon Oct 08, 2007 4:53 am
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

Posted: Mon Oct 08, 2007 6:23 am
by Stryks
What have you got set up so far?

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

Cheers

Posted: Mon Oct 08, 2007 6:50 am
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

Posted: Mon Oct 08, 2007 8:06 am
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)

Posted: Mon Oct 08, 2007 8:27 am
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....

Posted: Mon Oct 08, 2007 6:36 pm
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.

Posted: Tue Oct 09, 2007 4:54 am
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