Hi Guys/Girls,
The following function works fine, until you reach the maximum stock level.
If for instance you have 10 of the same t-shirt in your cart (and the stock level for said t-shirt is 10) and then try to add another it will give the user a message saying it has been added, however it won't update the quantity in the cart.
I'm unsure as to why it is telling the user it has been added because as you can see the if statement checks the quantity.
However, If i uncomment the exit; function at the bottom it works.
Here's the code.
<?php
function addToCart($pid, $size) {
$newcart = array();
$newitem = TRUE;
$cart = $_SESSION['cart'];
$ssize = $size;
foreach ($cart as $checkcart) {
$newqty = $checkcart[2];
$ssize = $checkcart[1];
if ($checkcart[0] == $pid && $checkcart[1] == $size) {
if ($this->getQuantityInStock($pid, $size)-1 >= $newqty) {
$newqty++;
}
else {
Header("Location: cart.php?error=nostock");
}
$newitem = FALSE;
}
$newcart[] = array($checkcart[0], $ssize, $newqty);
}
if ($newitem) {
$newcart[] = array($pid, $size, 1);
}
// exit;
$_SESSION['cart'] = $newcart;
}
?>
Thanks in advance