简体   繁体   中英

Updating quantity in shopping cart for one product resets the previously updated quantity of other products

I manage to change the quantity in my shopping cart for one product using a drop-down quantity menu and this.form.submit() but if I have two products in the shopping cart with updated quantity and I decide to update one of them again the other product's quantity get overridden and brought back to its previous, original value (before updating) which was sent using "Add to basket" from the page where all products are listed.

When I update the quantity of a product how do I preserve the already updated quantity of other products?

<?php
// Add products to basket:

if (!isset($_SESSION['basket'])) {
  $_SESSION['basket'] = array();
}

if (isset($_POST['id'])) {
  $_SESSION['basket'][$_POST['id']] = array(
    'product_id'=>$_POST['id'],
    'product_photo'=>$_POST['hidden_photo'],
    'product_photo_alt'=>$_POST['hidden_photo_alt'],
    'product_name'=>$_POST['hidden_name'],
    'product_price'=>$_POST['hidden_price'],
    'product_quantity'=>$_POST['quantity'],
  );
}
?>

<?php
// 
 if(!empty($_SESSION['basket'])) {
   $total = 0;
   foreach ($_SESSION['basket'] as $key => $value) {
// Below is the code which updates the quantity of a product with a particular id
// But after submitting the form resetes the quantity of other products 
// With already updated quantity 
     if (!empty($_POST['new_qty'][$value['product_id']])) {
       $quantity = $_POST['new_qty'][$value['product_id']];
     } else {
       $quantity = $value['product_quantity'];
     }
?>

<form method="post" action="">
<select class="quantity-basket" name="new_qty[<?php echo $value['product_id']; ?>]" onchange="this.form.submit()">

<?php
// Quantity added to the basket:                        
if ($quantity) {
?>

<option value="<?php echo $quantity; ?>" hidden><?php echo $quantity; ?></option>

<?php
}
?>

<?php
// Drop-down quantity menu
for ($i = 0; $i <=50; $i++) {
?>

<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">

<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>

</option>

<?php
}
?>

</select>
<!-- End of quantity drop-down menu -->
</form>

购物车截图

I've sorted it out thanks to this tutorial: https://www.allphptricks.com/simple-shopping-cart-using-php-and-mysql/#comment-79810

<?php

if (!isset($_SESSION['basket'])) {
  $_SESSION['basket'] = array();
}

if (isset($_POST['id'])) {
  $_SESSION['basket'][$_POST['id']] = array(
    'product_id'=>$_POST['id'],
    'product_photo'=>$_POST['hidden_photo'],
    'product_photo_alt'=>$_POST['hidden_photo_alt'],
    'product_name'=>$_POST['hidden_name'],
    'product_price'=>$_POST['hidden_price'],
    'product_quantity'=>$_POST['quantity'],
  );
}

// Update quantity
if (isset($_POST['action']) && $_POST['action'] == 'update') {
  foreach($_SESSION['basket'] as &$value) {
    if($value['product_id'] === $_POST['type']) {
        $value['product_quantity'] = $_POST['new_qty'];
        break; // Stop the loop after we've found the product
    }
  }
}
?>

<?php
// 
 if(!empty($_SESSION['basket'])) {
   $total = 0;
   foreach ($_SESSION['basket'] as $product) {
?>

<form method="post" action="">
<input type="hidden" name="type" value='<?php echo $product["product_id"]; ?>'>
<input type="hidden" name="action" value="update">
<select class="quantity-basket" name="new_qty" onchange="this.form.submit()">
<option value="<?php echo $product['product_quantity']; ?>" hidden><?php echo $product['product_quantity']; ?></option>

<?php
// Drop-down quantity menu
for ($i = 0; $i <=50; $i++) {
?>

<option value="<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>">

<?php echo ($i == 0) ? $i . ' (Remove)' : $i; ?>

</option>

<?php
}
?>

</select>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM