Page 1 of 1

PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 4:26 am
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

Re: PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 4:41 am
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

Re: PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 4:45 am
by cjconnor24
Hi,

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

Thanks for reply!

Re: PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 5:04 am
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.

Re: PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 6:07 am
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)

Re: PHP Shopping Cart Help - Product Option

Posted: Tue Aug 11, 2009 6:13 am
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