Hello all-
I am trying to store more info about each product in a session array.
The current shopping cart script that I am using is very basic. It totals the item price and adds up the quantity for each product.
I would like to include a discount field as well as the quantity. Calculating this is the easy part. Setting a session variable with : the ProductID, the Quantity, and the Discount is where the trouble begins.
Is this a multidemenisional array? It seems like it might be. How do I set the vaiable and then break it out.
View the folowing code to see what I am talking about.
First Notice how the tet box for the quantity is named after the product ID.
I put the discuont field in there but don't know what to do with it.
foreach ($cart as $productID => $qty)
{
$product = get_product_details($productID);
$discount = $HTTP_SESSION_VARS['discount'];
echo '<tr>';
echo '<td align=left><a href="product_detail.php?productID='.$productID.'">'.$product['title'].'</a></td>';
echo '<td align=center>$'.number_format($product['price'], 2).'</td>';
echo "<td align=center><input type=\"text\" name=\"$productID\" value=\"$qty\" size=\"2\"></td>";
echo "<td align=center><input type=\"text\" name=\"discount[]\" value=\"$discount\" size=\"4\">%</td>";
echo '<td align=right>$'.number_format($product['price']*$qty,2).'</td>';
echo '</tr>';
}
So, that's that part.
Now here is the code that adds a product to the cart (adds to the session vaiable called 'cart'). This is where the quantity is set as well.
if($new)
{
//new item selected
if(!isset($HTTP_SESSION_VARS['cart']))
{
$HTTP_SESSION_VARS['cart'] = array();
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['total_price'] ='0.00';
}
if(isset($HTTP_SESSION_VARS['cart'][$new])) {
$HTTP_SESSION_VARS['cart'][$new]++;
} else {
$HTTP_SESSION_VARS['cart'][$new] = 1;
}
$HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
}
if(isset($HTTP_POST_VARS['save']))
{
///////////////////
$i=0;
foreach ($HTTP_SESSION_VARS['cart'] as $productID => $qty) {
if($HTTP_POST_VARS[$productID]=='0') {
unset($HTTP_SESSION_VARS['cart'][$productID]);
} else {
$HTTP_SESSION_VARS['cart'][$productID] = $HTTP_POST_VARS[$productID];
$HTTP_SESSION_VARS['cart'][$product]['discount'] = $HTTP_POST_VARS[$discount];
echo $HTTP_SESSION_VARS['cart'][$discount].'<br>';
}
$i++;
}
$HTTP_SESSION_VARS['discount'] = calculate_discount($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']);
}
if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) {
display_cart($HTTP_SESSION_VARS['cart']);
} else {
echo '<p>There are no items in your cart</p>';
echo '<hr />';
}
Here are the functions that set the session vaiables as well. Sorry to post so much, but better than describing.
function calculate_price($cart)
{
// sum total price for all items in shopping cart
$price = 0.0;
if(is_array($cart))
{
foreach($cart as $productID => $qty) {
//echo $productID.'---'.$qty.'<br>';
$result = mysql_query("select price from products where productID='$productID'");
if ($result) {
$item_price = mysql_result($result, 0, 'price');
$price +=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
// sum total items in shopping cart
$items = 0;
if(is_array($cart))
{
foreach($cart as $productID => $qty) {
$items += $qty;
}
}
return $items;
}
function calculate_discount($cart)
{
// sum total items in shopping cart while (list ($key, $value) = each ($cart[$productID]['discount']))
$discount = 0;
if(is_array($cart))
{
foreach($cart as $productID => $disc) {
$discount += $disc;
}
}
return $discount;
}
Any ideas?
Please help.
Thank you!
Brandon