I cannot figure this out for the life of me. I am trying to add "item condition" variations, and "extra accessories" variations (kind of like how you would add color or size) when adding a product to cart. In addition, the price associated with the product is pulled from the database but calculated in the script depending on the condition and accessories the user selects. Therefore, in the cart page, I would like to display the product name, picture, final price (different from price in database), accessories, and item condition. I've attached a picture of the product page for reference to see exactly what I'm trying to associate with a product added to cart.[ATTACH]4791[/ATTACH]
And if I'm completely off, please let me know and I'll find a developer to finish the project. If I'm getting close, please give me some encouragement to solve this damn thing. Any help is much appreciated. Thank you in advance!
So here's the script:
Add to cart button on products page:
<?php
$query = "SELECT * FROM phones WHERE name = '{$field_Phone}'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$output[] = '<a href="cart-demo/cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
?>
As for the Add to cart link I've tried adding the variables in the link as follows. A. not even sure if this is correct, B. have no idea what to do in functions script to make that price associated with the product. Do I need a table for item conditions and another table for accessories?
$output[] = '<a href="cart-demo/cart.php?action=add&id='.$row['id'].'&price='.$price'">Add to cart</a></li>';
Cart Functions:
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return 'Cart Empty';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return 'Cart (<a href="cart-demo/cart.php">'.count($items).'</a>)';
}
}
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM phones WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$sql = 'SELECT * FROM condition WHERE id = '.$condition;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = "<td>" .$name. "</td>";
$output[] = "<td>" .$condition. "</td>";
$output[] = "<td><img src=\"../images/catalog-images/" .$photo_image. "\" width=\"50\"/></td>";
$output[] = '<td> $ '.$base_price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td> $'.($base_price * $qty).'</td>';
$total += $base_price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>$ '.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>
Checkout Page:
$cart = $_SESSION['cart'];
$action = $_GET['action'];
$_GET['id'];
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;
Screen Shot 2012-12-24 at 3.51.03 PM.png