Jump to content

Simple PHP Shopping Cart


htorbov

Recommended Posts

Hi!

 

I use the following PHP Session Shopping Cart (it's ready to use)

<?
session_start();

function showCart() {
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        foreach ($contents as $id=>$qty) {
            $output[] = 'You have '.$qty.' items with ID: '.$id;
        }
    }
    else {
        $output[] = 'You don\'t have items in your cart';
    }
    return join('',$output);
}

if(isset($_SESSION['cart'])) {
    $cart = $_SESSION['cart'];
}
else {
    $cart = 0;
}

if(isset($_GET['action'])) {
    $action = $_GET['action'];
    switch ($action) {
        case 'add':
            if ($cart) {
                $cart .= ','.$_GET['id'];
            } else {
                $cart = $_GET['id'];
            }
            break;
        case 'delete':
            if ($cart) {
                $items = explode(',',$cart);
                $newcart = '';
                foreach ($items as $item) {
                    if ($_GET['id'] != $item) {
                        if ($newcart != '') {
                            $newcart .= ','.$item;
                        } else {
                            $newcart = $item;
                        }
                    }
                }
                $cart = $newcart;
            }
            break;
        case 'update':
        if ($cart) {
            $newcart = '';
            foreach ($_POST as $key=>$value) {
                if (stristr($key,'qty')) {
                    $id = str_replace('qty','',$key);
                    $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                    $newcart = '';
                    foreach ($items as $item) {
                        if ($id != $item) {
                            if ($newcart != '') {
                                $newcart .= ','.$item;
                            } else {
                                $newcart = $item;
                            }
                        }
                    }
                    for ($i=1;$i<=$value;$i++) {
                        if ($newcart != '') {
                            $newcart .= ','.$id;
                        } else {
                            $newcart = $id;
                        }
                    }
                }
            }
        }
        $cart = $newcart;
        break;
    }
}

$_SESSION['cart'] = $cart;

echo showCart();
echo '<br/>';
echo '<a href="?action=add&id=1">Add to Cart</a>';
?>

 

Example of usage:

===

<a href="cart.php?action=add&id=1">Add to Cart</a> // Adding a new product in the cart with ID 1

<a href="cart.php?action=delete&id=1">Delete from Cart</a> // Deleting a new product in the cart with ID 1

<a href="cart.php?action=update&id=1&qty=5">Update Quantity to 5</a> // Updating the quantity of product with ID 1

===

 

Everything is fine, but now I want to add sizes, something like: cart.php?action=add&id=1&size=XXXL.

 

For example, the case "add" can be done like that:

$cart = $_GET['id'].":".$_GET['size'];

..  and then the products will be saved like 1:XXXL, but then the updating and deleting will not work ..

 

Please help me, I'm very confused how this can be done

Link to comment
https://forums.phpfreaks.com/topic/257467-simple-php-shopping-cart/
Share on other sites

I would store the contents of the cart in an array instead of a string. You can work on it a lot easier and you don't have to use explode a bunch of times. When you add something it might look like,

$id = $_GET['id'];
$size = $_GET['size'];
$qty = $_GET['qty'];

$cart[$id] = array('size' => $size, 'qty' => $qty);

 

This way, you can add as much additional information about the item as you want with very little effort.

You should try to split up the tables and normalize as best you can. For example, you might have a products table holding all of the product names. Then have a cart table, holding a user id, product id, and some options like size, color, quantity etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.