Himy name is Leo and i'm creating a shoppping cart.I created the shopping cart and the database for the products, but now i want to put some restriction for the height, width, length, weigth for the box of the products to calculate the freight(i already have the code to calculate the freight i just need the comparison to put the values there with a different comparison for each id).
For exemple: I add a product in the cart and the quantity is 1 so take my values from the database for this product, but if i increase the quantity for 50 or greater then 50 it takes my box values(height, width, legth, weigth), and multiple for 2.
If there is some that could help, and thanks all of you for your time and support.
Here is my cart code(the website is for a store in Brazil so the final part of the code some input names is in portuguese for their mail freight calculator, but the rest of the code is in english and the part in portuguese doesn't have nothing to do for what i need, thank you):
<?php
session_start();
include_once("connecting.php");
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
// Add product
if(isset($_GET['action']))
{
if($_GET['action'] == 'add')
{
$id = intval($_GET['id']);
if(!isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id] = 1;
}
else
{
$_SESSION['cart'][$id] += 1;
}
}
// remove product
if($_GET['action'] == 'remove')
{
$id = intval($_GET['id']);
if(isset($_SESSION['cart']));
{
unset($_SESSION['cart'][$id]);
}
}
// update cart
if($_GET['action'] == 'update')
{
if(is_array($_POST['prod']))
{
foreach($_POST['prod'] as $id => $quantity)
{
$id = intval($id);
$quantity = intval($quantity);
if(!empty($quantity) || $quantity <> 0)
{
$_SESSION['cart'][$id] = $quantity;
}
else
{
unset($_SESSION['cart'][$id]);
}
}
}
}
print_r ($_SESSION['cart']);
}
function formatValue($value)
{
return number_format($value, 2, ",", ".");
}
?>
<form action="?action=update" method="post">
<table width="800" border="1" align="center">
<tr>
<th colspan="6">Cart</th>
</tr>
<tr>
<td>Id</td>
<td>Product</td>
<td>Quantity</td>
<td>Price</td>
<td>SubTotal</td>
<td>Remove</td>
</tr>
<?php
$pp_cart = "";
if(count($_SESSION['cart']) == 0)
{
echo '<tr> <td colspan="6">Your cart is empty.</td> </tr>';
$total = 0;
}
else
{
$pp_cart .= '<form action="http://ift.tt/xpRUtH" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="vendedortest@test.com">
';
$total = 0;
foreach($_SESSION['cart'] as $id => $quantity)
{
$sql = "SELECT * FROM produtos WHERE id = '$id'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
$product = $row['name'];
$price = $row['price'];
$id = $row['id'];
$height = $row['height'];
$width = $row['width'];
$length = $row['length'];
$weight = $row['weight'];
$cepOrigem = $row['cepOrigem'];
$sub = $price * $quantity;
$total = $total + $sub;
// echo table
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$product</td>";
echo '<td><input type="number" name="prod['.$id.']" value="'.$quantity.'" min="0"></td>';
echo '<td>'.formatValue($price).'</td>';
echo '<td>'.formatValue($sub).'</td>';
echo "<td><a href='cart.php?action=remove&id=$id'>Remove</a></td>";
echo "</tr>";
}
if(!isset($pp_item))
{
$pp_item = 1;
}
else
{
$pp_item = $pp_item + 1;
}
$pp_cart .= '<input type="hidden" name="item_name_'.$pp_item.'" value="' .$produto. '">
<input type="hidden" name="item_number_'.$pp_item.'" value="' .$id. '">
<input type="hidden" name="amount_'.$pp_item.'" value="' .$price. '">
<input type="hidden" name="quantity_'.$pp_item.'" value="' .$quantity. '">
';
} // fecha foreach
$pp_cart .= '<input type="hidden" name="currency_code" value="BRL">
<input type="hidden" name="lc" value="BR">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="return" value="products.php">
<input type="hidden" name="cancel_return" value="cart.php">
<input type="hidden" name="notify_url" value="paypal.php">
<input type="image" src="http://ift.tt/1dAbTnx" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>
';
}
?>
<tr>
<td colspan="2"><a href="products.php">Continue Shopping</a></td>
<td colspan="2"><input type="submit" value="update cart" /></td>
<td>R$ <?php echo formatValue($total); ?></td>
</tr>
</table>
</form>
Aucun commentaire:
Enregistrer un commentaire