John Davidson

php - Problem with shopping cart adds same product multiple time

0 comments
Message:


I have php mysql shopping cart which works well, but problem is that if I add product which has code numbers only without any letter, product is added multiple time.
For example product code: K001 is working ok, but if product code is 001 without K I have same problem.


$cartArray = array(
$code=>array(
'name'=>$name,
'code'=>$code,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);

if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($array_keys, $code)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}

}
}
?>



I added please take a look, but have same problem


$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$code = strval($row['code']);
$price = $row['price'];
$image = $row['file_name'];

$cartArray = array(
$code=>array(
'name'=>$name,
'code'=>$code,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);


My Answer:
$_POST['code'];
$name=$_POST['name'];
$price=$_POST['price'];
$quantity=$_POST['quantity'];

$cartArray = array(
'code' => $code,
'name' => $name,
'price' => $price,
'quantity' => $quantity
);

if(!empty($_SESSION['shopping_cart'])) {
$array_keys = array_keys($_SESSION['shopping_cart']);
if(in_array($code, $array_keys)) {
// Product already exists in cart, update quantity
$_SESSION['shopping_cart'][$code]['quantity'] += $quantity;
} else {
// Product does not exist in cart, add it
$_SESSION['shopping_cart'][$code] = $cartArray;
}
} else {
// Cart is empty, add product
$_SESSION['shopping_cart'][$code] = $cartArray;
}

This code snippet checks if the product code already exists in the shopping cart. If it does, it updates the quantity of the existing product. If not, it adds the new product to the cart.

The issue you are facing with products that have code numbers only (e.g. "001") may be due to the way PHP handles array keys. PHP treats array keys as strings, so if you have a product with a code like "001", PHP may treat it as the integer 1. This can cause the product to be added multiple times if there are other products with similar numeric codes.

To fix this issue, you can modify the code to ensure that the product code is always treated as a string. You can do this by explicitly casting the code to a string before using it as an array key:

$code = (string)$_POST['code'];

This will ensure that the product code is always treated as a string, regardless of whether it contains letters or not.

Rate this post

4 of 5 based on 9707 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us