Hi all,
I am having trouble with my code so hopefully one of you can help!
My code below adds items to a shopping cart (session variables). The problem I am having is that if I have one item in the cart already and I choose to add 2 more items (as an example), the code doesn't update the session variable to be 3 items in total, it seems to reset the variable and updates to be 2 items instead of 3.
Can anyone identify/highlight the code which is causing me a headache!
if (!empty($_POST['quantity'])) {
$quantity = $_POST['quantity'];
$productid = $_POST['productid'];
$productprice = $_POST['productprice'];
$selquantitycount = count($_SESSION['S_SELECTED_QUANTITIES']);
for ($i = 0; $i <= $quantity; $i++) {
if ($quantity > 0) {
$_SESSION['S_SELECTED_PRODUCTS'] = $productid;
$_SESSION['S_SELECTED_QUANTITIES'] = $quantity;
$_SESSION['S_SELECTED_PRODUCT_PRICES'] = $productprice;
$selquantitycount++;
}
}
}
$totalordervalue = CalculateOrderValue(); // this is the function shown below...
And here is the CalculateOrderValue function...
function CalculateOrderValue() {
$quantitytotal = 0;
$pricetotal = 0;
$selquantitycount = count($_SESSION['S_SELECTED_PRODUCT_PRICES']);
for ($i = 0; $i <= $selquantitycount; $i++) {
$quantity = $_SESSION['S_SELECTED_QUANTITIES'];
$price = $_SESSION['S_SELECTED_PRODUCT_PRICES'];
$itemprice = number_format($quantity * $price, 2);
$quantitytotal = number_format($quantity, 0);
$pricetotal = $itemprice;
}
$totalordercost = $pricetotal;
$_SESSION['S_TEMP_BASKET_QUANTITY'] = $quantitytotal;
$_SESSION['S_BASKET_TOTAL_QUANTITY'] = $quantitytotal;
$_SESSION['S_BASKET_TOTAL_PRICE'] = $totalordercost;
return $totalordercost;
}
Thanks in advance.