我设法使用下拉数量菜单更改我的购物车中一种产品的数量,但是this.form.submit()如果我在购物车中有两种具有更新数量的产品并且我决定再次更新其中一种,另一种产品的数量将被覆盖并且恢复到其之前的原始值(更新前),该值是使用列出所有产品的页面上的“添加到购物车”发送的。
当我更新一个产品的数量时,如何保留其他产品已经更新的数量?
<?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>
湖上湖