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

wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Shopping Cart

Post by wizzard »

Hello,

I'm working on a webshop for my site. It's a shop for Audio products but i want if the user added something to the shopping cart if he visits back the site 5hours later it still is in the basket.

If possible can somebody gives me some examples or scripts. I'm newbie and have to finish this before july for my website. I already made the tables with products, ... but i don't understand to start with this cookies or sessions.

Cheers
Kris
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

To do this, then I'd either keep a cookie keeping track of the products on the user's computer (if they aren't a member) or store the user's cart in the database (if they are a member).
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

Can you maybe give me an example of code?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Setting a cookie is fairly easy:
http://www.php.net/manual/en/function.setcookie.php

If you are storing the contents of their shopping cart in a database table then all you need is some unique identifier linked to them that you can store both in the database and the cookie.

Mac
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

What can a unique identifier be? Session ID not because if you open another browser screen you have a new session so what i can put as identifier because i don't want that customers have to sign up first.

Cheers
Kris
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

opening a new window of the same browser should not change the session-state unless it's a very bad browser ;)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

A cookie with a randomly generated value (script below if you need one) could identify different shoppers without requiring them to input anything.

Code: Select all

// random string of numbers and upper/lower case characters of length given by, er, $length
function random_numchar($length) {
    $chars = array_merge(range(a, z), range(0, 9));
    $counter = 0;
    while ($counter < $length) {
        $x = mt_rand(0, 35);
        $char = $chars[$x];
        $y = mt_rand(1, 2);
        IF ($y % 2 == 0 AND ctype_alpha($char)) {
            $char = strtoupper($char);
        }
        $str .= $char;
        $counter++;
        }
    return $str;
}
Last edited by McGruff on Thu Aug 11, 2005 12:34 pm, edited 1 time in total.
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

thanks i understand the random id is putted in the cookie and in the table.
When the user comes back i have to grab the id from the cookie and check it with the id in the table.

Thanks guys for all the help.

Regards
Kris
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

Little question if the users have disabled cookies in their browser then the shopping cart want work?
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Updating Shopping Cart

Post by wizzard »

Can someone help me with an update function i have a problem:

when i use the function for viewing the cart i put the quantity like this
<input type=\"text\" name=\"qty\" size=\"2\" value=\"$qty\">

The value $qty comes out from the database but if i update it doesn't works because it still takes the value from the $qty from the database.

Now i want is when a user changed the quantity in the text field it also will be updated. Can someone helps me?

Regards
Kris
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You'd have to have a bit of code that set $qty to the posted value if set, or otherwise to the db value, maybe something like this (assuming that you can access the db value as $row['qty']):

Code: Select all

$qty = (isset($_POST['qty'])) ? $_POST['qty'] : $row['qty'];
Mac
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

But it's like when you have 6 products in the shopping cart

and you change the quantity from the 2 and 4th one how can you make a function that doing this when pressing the recalculate button.

Regards
Kris
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Post by wizzard »

that's my problem i have:

When you have 6 products in the shopping cart

and you change the quantity from the 2 and 4th one how can you make a function that doing this when pressing the recalculate button.
wizzard
Forum Commoner
Posts: 93
Joined: Thu May 16, 2002 5:36 am
Location: Belgium
Contact:

Need Help!

Post by wizzard »

I really need some help with my update function.

How can the function Update() knows what products that has to be updated i mean

Like this is the shopping cart.

Qty Product Name: Price

2 DMD RED - Reasonfreaks Edition ? 159.9
1 DMD YELLOW - Construction Ki t ? 74.95

When the user change the quantity of product 1 and 2 and press recalc then the qty has to been changed. But i really don't understand how i can make the function and how i need to call the function.

Like every row in the shopping cart needs an unique quantity field maybe the product code as field? like SMP-0000001 SMP-0000002

But then how can the function know all the fields. I hope you can help me because i have to finish my shop before june.

Regards
Kris
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Is this shopping cart going to be used on a live commerce site? Forgive me for being blunt but I'd be concerned that, if you are struggling with the basic code, how well do you understand the security required to deal with credit card numbers? You need a fairly advanced knowledge level to with all that properly. It's bad for your business if CCs get hacked - and bad for the reputation of the internet & php as a whole as a safe way to do business.

Don't get me wrong: I don't want to put you off. As long as you bone up on all the issues you'll get there in the end.

Now that my standard rant is out of the way, do you know how to make a form / form processor script? If not, I'll post an example later today (after that I'll be away for a bit so be quick!).
Post Reply