i am working on this ecommerce website and i am trying to figure out how to add the shipping information to the total.
can someone please help me to get this to work? thanks.
here is an example of what i'm trying to accomplish:
For example, if I buy three guitars, I pay $15 for shipping but if I buy two guitars, I pay $10 for shipping ($5 per item).
here's my website that i'm working on, i'm working on the shopping cart section, if you add a product to the cart, it'll take you to the cart page.
http://www.wootenmedia.com/wootenmusic8/
here's the code that i have that i'm working with:
<?php
include("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"], $_GET["catid"], $_GET["proditems"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"], $_GET["catid"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"], $_GET["catid"]);
ShowCart();
break;
}
case "delete_items":
{
DeleteItems($_GET["id"], $_GET["qty"], $_GET["catid"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty, $catid)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId =' $itemId' and catid = '$catid'");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
@mysql_query("insert into cart(cookieId, itemId, qty, catid) values('" . GetCartId() . "', $itemId, $qty, $catid)");
}
else
{
UpdateItem($itemId, $qty, $catid);
}
}
function UpdateItem($itemId, $qty)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
if($qty == 0)
{
RemoveItem($itemId);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function DeleteItems($itemId, $qty, $catid)
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "'");
}
function ShowCart()
{
global $dbServer, $dbUser, $dbPass, $dbName;
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$totalCost = 0;
$shipping = "3.49";
$ship = "5.00";
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
?>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="7%" height="25" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Qty</strong>
</font>
</td>
<td width="10%" height="25" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Products</strong>
</font>
</td>
<td width="10%" height="25" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Product</strong>
</font>
</td>
<td width="10%" height="25" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Price Each</strong>
</font>
</td>
<td width="10%" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Total</strong>
</font>
</td>
<td width="10%" height="25" bgcolor="#50535C">
<font face="Arial" size="2" color="white">
<strong>Remove?</strong>
</font>
</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="7%" height="25" valign="top">
<font size="2" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="10%" height="25" valign="top">
<font size="2" color="black">
<?php echo $row["prodItems"]; ?>
</font>
</td>
<td width="10%" height="25" valign="top">
<font size="2" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25" valign="top"> <font size="2" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?></font>
<br>
</td>
<td width="10%" height="25" valign="top"> </td>
<td width="10%" height="25" valign="top">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>"><img src="imglinks/remove.jpg" width="51" height="13" border="0"></a>
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td> </td>
<td colspan="3" valign="top" width="10%" align="right"><font size="2">Shipping
Cost is $3.49 added to Total Amount</font> <br>
</td>
<td colspan="2">
<font size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font><br>
<?
echo "Total + Shipping = <br>$$total";
echo "<br><br>";
//$ship2 = $row["itemPrice"];
//$ship3 = $ship2 + $ship;
//echo "$ship3";
//$1 = number_format($totalCost, 2, ".", ",");
//$3 = $1 + $ship;
//echo "$3";
echo $row["qty"];
echo $row["itemPrice"];
echo "<br><br>";
$totalCost2 = $row["qty"] * $row["itemPrice"];
echo "totalcost2 is $totalCost2 <br><br>";
$sh = $totalCost2 + $ship;
echo "NEWEST $sh <br><br>";
$ship2 = $totalCost + $ship;
echo "new shipping is $5.00 per item <br><br> New shipping is $ship2";
echo "<br><br>";
?>
<b>NEW Total: $<?php echo number_format($ship2, 2, ".", ","); ?></b>
<br /><br />
</td>
</tr>
<tr>
<td width="100%" colspan="6">
<hr size="1" color="#1B1987" NOSHADE>
</td>
</tr>
<!--<tr>
<td width="50%" colspan="6"> </td>
<tr>-->
<td colspan="6" align="left">
<a href="index.php"><img src="images/keepshop.jpg" width="150" height="20" alt="" border="0"></a>
<a href="login.php"><img src="images/checkout.jpg" width="110" height="20" alt="" border="0"></a>
<br />
</td>
</tr>
<tr>
<td width="50%" colspan="6" align="left">
<a href="cart.php?action=delete_items&id=<?php echo $row["itemID"]; ?>"><img src="images/removeall2.jpg" width="160" height="20" alt="" border="0"></a><br /><br />
</td>
</tr>
</table>
</form>