Hi all, I’m a new user. I have a shopping cart php script that works fine.
I made changes by adding different variants to each product.
Now when I add the product by selecting the variants it works perfectly.
However, if I try to add the same product but with different variants, the previous product is overwritten and only the newly added product remains.
I would like both products to stay.
I apologize if my english is not correct but I use google translate.
Here is the code that opens the cart sessions:
session_start();
//check for additions
if (isset($_POST['aggiunta'])){
// original string
$OriginalString = $_POST['aggiunta'];
$matches = implode(',', $OriginalString);
$sql = "SELECT * FROM aggiunte WHERE aggiunta_id IN ($matches)";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$descrizione_aggiunta[]=$row["descrizione_aggiunta"];
$valore_aggiunta[]=$row["valore_aggiunta"];
$aggiunta_id[]=$row["aggiunta_id"];
$tipo_aggiunta[]=$row["tipo_aggiunta"];
$somma +=$row["valore_aggiunta"];
$_SESSION['aggiunta_id'][]=$aggiunta_id;
$new_product['valore_aggiunta']=$valore_aggiunta;
$new_product['descrizione_aggiunta']=$descrizione_aggiunta;
$new_product['aggiunta_id']=$aggiunta_id;
}
}
}
if(isset($_POST["remove_code"]) OR isset($_POST["product_qty"]) && isset($_SESSION['coupon']))
{
unset($_SESSION['coupon']);
}
//add product to session or create a new one
if(isset($_POST["type"]) && $_POST["type"]=='add' && $_POST["product_qty"]>0 )
{
unset($_SESSION['coupon']);
foreach($_POST as $key => $value){ //aggiungi tutte le variabili post all'array new_product
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
unset($new_product['type']);
unset($new_product['return_url']);
//we need to get product name and price from database.
$statement = $conn->prepare("SELECT coupon_name,prezzo_reale,acconto,coupon_shop,multi_deal,gratis FROM coupons_coupons WHERE coupon_id=? LIMIT 1");
$statement->bind_param('s', $new_product['product_code']);
$statement->execute();
$statement->bind_result($coupon_name,$prezzo_reale,$acconto,$coupon_shop,$multi_deal,$gratis);
while($statement->fetch()){
//fetch product name, price from db and add to new_product array
$new_product["coupon_name"] = $coupon_name;
$new_product["multideal"] = $multi_deal;
$new_product["coupon_shop"] = $coupon_shop;
$prodotto=$_POST['product_code'];
$new_product["product_price"] = $prezzo_reale+$somma;
if(isset($_SESSION['shop'])){
}
else
{
$_SESSION['shop'][] = $new_product["coupon_shop"];
}
if(isset($_SESSION["cart_products"])){ //if the session var already exists
if(isset($_SESSION["cart_products"][$new_product['product_code']])) //check that the item exists in the products array
{
unset($_SESSION["cart_products"][$new_product['product_code']]); //delete the old element of the array
}
}
$_SESSION["cart_products"][$new_product['product_code']] = $new_product; //refresh or create a product session with a new item
}
}
//update or remove items
if(isset($_POST["product_qty"]) || isset($_POST["remove_code"]))
{
//update item quantity in product session
if(isset($_POST["product_qty"]) && is_array($_POST["product_qty"])){
foreach($_POST["product_qty"] as $key => $value){
if(is_numeric($value)){
$_SESSION["cart_products"][$key]["product_qty"] = $value;
}
}
}
//remove an item from product session
if(isset($_POST["remove_code"]) && is_array($_POST["remove_code"])){
foreach($_POST["remove_code"] as $key){
unset($_SESSION["cart_products"][$key]);
unset($new_product['multideal']);
unset($_SESSION['descrizione_opzione']);
}
}
}
//back to return url```
Thanks to anyone who wants to help me I'm still learning.