Page 1 of 2

Shopping Cart

Posted: Sun Mar 30, 2003 9:45 am
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

Posted: Sun Mar 30, 2003 10:22 am
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).

Posted: Sun Mar 30, 2003 10:53 am
by wizzard
Can you maybe give me an example of code?

Posted: Sun Mar 30, 2003 1:22 pm
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

Posted: Sun Mar 30, 2003 2:52 pm
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

Posted: Sun Mar 30, 2003 4:10 pm
by volka
opening a new window of the same browser should not change the session-state unless it's a very bad browser ;)

Posted: Sun Mar 30, 2003 5:51 pm
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;
}

Posted: Mon Mar 31, 2003 2:30 am
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

Posted: Tue Apr 01, 2003 10:32 am
by wizzard
Little question if the users have disabled cookies in their browser then the shopping cart want work?

Updating Shopping Cart

Posted: Wed Apr 02, 2003 4:27 am
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

Posted: Wed Apr 02, 2003 4:37 am
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

Posted: Wed Apr 02, 2003 6:01 am
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

Posted: Thu Apr 03, 2003 6:51 am
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.

Need Help!

Posted: Mon Apr 07, 2003 7:02 am
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

Posted: Mon Apr 07, 2003 10:10 am
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!).