PHP Shopping Cart Help - Product Option

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
cjconnor24
Forum Newbie
Posts: 13
Joined: Wed Oct 29, 2008 6:31 am

PHP Shopping Cart Help - Product Option

Post by cjconnor24 »

Hi all i hope you can help as i'm slowly tearing my hair out.

I have a working shopping cart which adds items to the sessions like so:

Code: Select all

 
// CHECK IF A PRODUCT IS BEING ADDED TO THE BASKET
if($_GET['action']=='add' || isset($_GET['id'])){
 
//GET THE PRODUCT ID
$id = $_GET['id'];
 
    //IF THE ITEM IS ALREADY IN THE CART - JUST ADD 1 TO QTY
    if(isset($_SESSION['cart'][$id])){
    $_SESSION['cart'][$id]++;
    
    //REDIRECT TO CART ONCE ADDED
    Header("Location: /dnp/cart/");
    ob_end_flush();
 
    } else {
    
    //IF IT ISN@T IN THE CART, JUST ADD IT ONCE
    $_SESSION['cart'][$id] = 1;
 
    //REDIRECT TO CART ONCE ADDED
    Header("Location: /dnp/cart/");
    ob_end_flush();
 
    }
 
}
 
My product table is structured like so:
id name description price

My question is how can i add options. For instance colours sizes, and the price may change depending on the options.

Probably need a new table which is fine - if someone could detail how to do this i would be ever so greatful!

I look forward to your replies!!

Thanks
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: PHP Shopping Cart Help - Product Option

Post by towerspl »

the table would be along the lines of e.g. size:

id name

then a pivot table to assign the sizes to the product:


id size_id product_id



then when adding the size to the shopping cart you will need to have a nested array e.g.:

$_SESSION['cart'][1]['size'] = 1
cjconnor24
Forum Newbie
Posts: 13
Joined: Wed Oct 29, 2008 6:31 am

Re: PHP Shopping Cart Help - Product Option

Post by cjconnor24 »

Hi,

I've never used a pivot table in PHP - do you know where i could find some tutorials?

Thanks for reply!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP Shopping Cart Help - Product Option

Post by onion2k »

Products don't actually have options. They have SKUs. Every product sold in a store should have it's own unique SKU (Stock Keeping Unit) ... so if you have a shirt that's available in 4 sizes and 5 colours that's actually 20 database records. That many is necessary because (eventually) you should be tracking the stock level of every SKU, with a barcode for each, etc. EG

Code: Select all

sku_id sku_code
1      Shirt_1_S_BLUE
2      Shirt_1_S_RED
3      Shirt_1_S_GREEN
4      Shirt_1_S_YELLOW
5      Shirt_1_S_PINK
6      Shirt_1_M_BLUE
7      Shirt_1_M_RED
8      Shirt_1_M_GREEN
9      Shirt_1_M_YELLOW
10     Shirt_1_M_PINK
11     Shirt_1_L_BLUE
12     Shirt_1_L_RED
13     Shirt_1_L_GREEN
14     Shirt_1_L_YELLOW
15     Shirt_1_L_PINK
16     Shirt_1_XL_BLUE
17     Shirt_1_XL_RED
18     Shirt_1_XL_GREEN
19     Shirt_1_XL_YELLOW
20     Shirt_1_XL_PINK
You'd relate them together with a product ID obviously. You only need to store the SKU that the user has added to their basket.
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: PHP Shopping Cart Help - Product Option

Post by towerspl »

if you google around you should find some tutorials on it but basically:

you will have a table for your sizes:

id name


then to assign the size to the product you will have another table(pivot table):

id size_id product_id

size_id being the id of the size in the sizes table and product_id being the product id you wish to assign the size to.

You could just add the product_id to the size table but by using a pivot table you can assign the size to multiple products(making life a lot easier)
cjconnor24
Forum Newbie
Posts: 13
Joined: Wed Oct 29, 2008 6:31 am

Re: PHP Shopping Cart Help - Product Option

Post by cjconnor24 »

Ahh ok i do know what a pivot table is then!!

That's great! Thank you so much for all your help - i will have a bash at both suggestions!! :D
Post Reply