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

    I can say your question is too large in scope. BUT I have dealt with exactly what you are referring to. So I'll give you my experience for the amount you are paying for it..

    I deal with this also, products with a matrix of options. Generally, you'll need some logic rules and possibly some mods to the session descriptions.

    Suppose the SKU was PH-535. this is some kind of phone
    NOW, you have axis 1 - colors - say there are 5
    NEXT, you have axis 2 - operating system - MacOSX, Windows XP, Windows Vista and let's say W7 costs 75.00 extra (totally winging it).
    You have a lot of ways to approach this.
    I personally would enter two products if a secondary cost was incurred. Fact is, if items sold required good accounting, I'd record ANY of the OS options as a second item.
    That means your cart writer needs logic to not calculate the cost of the sub-item (W7 OS), at least count-wise.

    Also, you're not going to be able to do this via a add link, instead you'll need a form to pull this off:

    ?>
    <form action="add_cart.php">
    <h4><?php echo $phone_name;?></h4>
    Select a color: <select name="color">
    <option value="BL">Blue</option>
    ..etc..
    </select>
    <br />
    Select an operating system: <select name="OS">
    <option value="WXP">Windows XP</option>
    <option value="MOSX">Mac OSX</option>
    <option value="W7">Windows 7</option>
    ..etc..
    </select>
    <input type="submit" value="Add Phone.." />
    
    </form>
    <?php
    

    then you're going to have to write the scripting that processes this and stores it in session.

    I suggest you thing through this, put it down on paper, and take it one step at a time. The little problems would be good items to first search on this forum, then post if you can't find anything already.

    I hope that points you in the right direction.

    Sam

      One more thing on the multiple checkboxes, do those as follows to store the data:

      <label><input name="option[]" value="Charger" /> Charger </label><br />
      <label><input name="option[]" value="Carrying Case" /> Carrying Case </label><br />
      you'll see what I mean when you print_r() the form post..

      Sam

        Write a Reply...