简体   繁体   English

为一种产品更新购物车中的数量会重置之前更新的其他产品的数量

[英]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.我设法使用下拉数量菜单和this.form.submit()更改我的购物车中一种产品的数量,但是如果我的购物车中有两种产品的数量已更新,我决定再次更新其中一种其他产品的数量会被覆盖并恢复到其先前的原始值(更新前),该值是使用列出所有产品的页面上的“添加到购物篮”发送的。

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感谢本教程,我已将其整理出来: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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