I want it to add the shirt size as well, so how would I do this, heres my code:
Heres my image/link to add the item:
<a href="store.php?page=add&id='. $prod->id .'&size=Medium" title="Add to Cart"><img src="/images/icon-cart-add.gif" alt="Add" /></a>
Heres my add page:
// If no product id is specified, go back to the main page.
$_SESSION["cart"]->add($id, $size, 1);
$_SESSION["cart"]->cleanup();
$_SESSION["cart"]->recalc_total();
if (! empty($HTTP_REFERER)) {
header("Location: store.php?page=cart");
} else {
header("Location: store.php?page=cart");
}
My add function:
Now how would I make that insert the shirt size? and maybe color?
I dont understand the function since I used a tutorial.
function add(&$productid, $shirtsize, $qty) {
// Add an item to the shopping cart and update the total price.
if (isset($productid)) {
setdefault($this->items[$productid], 0);
$this->items[$productid] += $qty;
}
}
Can someone please help:
Heres my full class function:
class Cart {
var $items; // Array of items
var $total; // Cart total
function Cart() {
$this->init();
}
function init() {
// This function is called to initialize (and reset) a shopping cart.
$this->items = array();
$this->total = 0;
}
function add(&$productid, &$shirtsize, $qty) {
// Add an item to the shopping cart and update the total price.
if (isset($productid)) {
setdefault($this->items[$productid], 0);
$this->items[$productid] += $qty;
$this->items[$shirtsize] += $shirtsize;
}
}
function remove(&$productid) {
// This function will remove a given product from the cart.
if (isset($productid)) {
unset($this->items[$productid]);
}
}
function cleanup() {
// This function will clean up the cart, removing items with invalid product id's (non-numeric ones) and products with quantities less than 1.
foreach ($this->items as $productid => $qty) {
if ($qty < 1) {
unset($this->items[$productid]);
}
}
}
function itemcount() {
// Returns the number of individual items in the shopping cart (note, this takes into account the quantities of the items as well).
$count = 0;
foreach ($this->items as $productid => $qty) {
$count += $qty;
}
return $count;
}
function get_productid_list() {
// Return a comma delimited list of all the products in the cart, this will be used for queries, eg. SELECT id, price FROM products WHERE id IN ....
$productid_list = "";
foreach ($this->items as $productid => $qty) {
$productid_list .= ",'" . $productid . "'";
}
// Need to strip off the leading comma.
return substr($productid_list, 1);
}
function recalc_total() {
// Recalculate the total for the shopping cart, we will also do some cleanup and remove invalid items from the cart.
// We have to query the database to get the prices, so instead of making one query for each product in the basket
// We will gather up all the ID's we are interested in and run one query to get all the products we care about (using $in_clause).
$this->total = 0;
$in_clause = $this->get_productid_list();
if (empty($in_clause)) {
return;
}
$qid = db_query("SELECT id, price FROM products WHERE id IN ($in_clause)");
while ($product = db_fetch_object($qid)) {
$this->total += $this->items[$product->id] * $product->price;
}
}
}
function get_cart_items() {
// Return a $qid of all the items in the shopping cart.
global $_SESSION;
$in_clause = $_SESSION["cart"]->get_productid_list();
if (empty($in_clause)) {
return false;
}
return db_query("SELECT * FROM products WHERE id IN ($in_clause)");
}
And this is the code that echos whats in the cart,
how would I make it show the size they chose?
<? load_profile();
$qid = get_cart_items();
while ($prod = db_fetch_object($qid)) {
$qty = $_SESSION["cart"]->items[$prod->id];
$total = $prod->price * $qty;
?>
