将相同产品添加到购物车后更新数量
Update quantity after adding same product to shopping basket
我不知道如何通过再次添加来更新已经在购物篮中的产品的数量。如果数量为 1,添加数量为 3 的相同产品后,应在购物篮中显示 4。我现在所能做的就是用新的数量替换添加的数量,或者在购物篮中插入一个空元素 table,没有任何产品详细信息。
<?php
// Products are added to the basket
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
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'])
);
}
}
// This is the code with which I can only insert an empty element in the basket table with no product details.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['id']) && isset($_POST['quantity'])) {
foreach ($_SESSION['basket'][$_POST['id']] as $item) {
if ($_item['product_id'] === $_SESSION['basket']['product_id']) {
$_SESSION['basket']['product_quantity'] += $item['product_quantity'];
}
}
}
}
// If I apply below code it just replaces the quantity:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['id']) && isset($_POST['quantity'])) {
foreach ($_SESSION['basket'] as $item) {
if ($_item['product_id'] === [$_POST['id']]) {
$item['product_quantity'] += $_POST['quantity'];
}
}
}
}
?>
如果您需要更多详细信息,请告诉我。
更新
我写了另一个代码,但这次它只是将发送到购物篮的数量增加了一倍。我还在列出产品的页面上添加了一个隐藏的输入字段。
列出产品和将产品添加到购物车的页面上的代码:
<form> // Submitted via Ajax
<div>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"/>
<input type="hidden" name="do_it" value="quantity_again"/> // New input field added
<select name="quantity">
<?php
// Drop-down select quantity menu
for ($i=1; $i<=$row['stock']; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select>
</div>
<input type="hidden" name="hidden_photo" value="<?php echo $row['img_front_url']; ?>"/>
<input type="hidden" name="hidden_photo_alt" value="<?php echo $row['img_front_alt'];?>"/>
<input type="hidden" name="hidden_name" value="<?php echo $row['product']; ?>"/>
<input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/>
<button class="add-to-basket">Add to basket</button>
</form>
新代码仅将购物篮中的数量翻倍。
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
foreach ($_SESSION['basket'] as &$item) {
if ($item['product_id'] === $_POST['id']) {
$item['product_quantity'] += $_POST['quantity'];
}
}
}
}
看来$item['product_quantity']
和$_POST['quantity']
是一样的,所以数量翻倍了。
我想我需要先检查 $_POST['quantity']
是否存在于数组 $_SESSION['basket']
中,如果存在,将其存储在一个变量中然后执行 $item['product_quantity'] += $stored_qty
但这只是再次覆盖数量。这是代码:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
foreach ($_SESSION['basket'] as $item) {
if (in_array($_POST['quantity'], $_SESSION['basket'])) {
$stored_qty = $_POST['quantity'];
if ($item['product_id'] === $_POST['id']) {
$item['product_quantity'] += $stored_qty;
}
}
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'change') {
if (isset($_SESSION['basket'])) {
$is_available = 0;
foreach ($_SESSION['basket'] as $keys => $values) {
if ($_SESSION['basket'][$keys]['product_id'] == $_POST['id']) {
$is_available++;
$_SESSION['basket'][$keys]['product_quantity'] += $_POST['quantity'];
}
}
}
if ($is_available == 0) {
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
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'])
);
}
}
}
}
}
归功于 https://www.webslesson.info/2018/04/shopping-cart-by-using-bootstrap-popover-with-ajax-php.html
尽管如此,我不确定我是否理解 $is_available
在代码中的实际作用,以及它如何指代添加相同产品时增加的数量。
我不知道如何通过再次添加来更新已经在购物篮中的产品的数量。如果数量为 1,添加数量为 3 的相同产品后,应在购物篮中显示 4。我现在所能做的就是用新的数量替换添加的数量,或者在购物篮中插入一个空元素 table,没有任何产品详细信息。
<?php
// Products are added to the basket
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
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'])
);
}
}
// This is the code with which I can only insert an empty element in the basket table with no product details.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['id']) && isset($_POST['quantity'])) {
foreach ($_SESSION['basket'][$_POST['id']] as $item) {
if ($_item['product_id'] === $_SESSION['basket']['product_id']) {
$_SESSION['basket']['product_quantity'] += $item['product_quantity'];
}
}
}
}
// If I apply below code it just replaces the quantity:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['id']) && isset($_POST['quantity'])) {
foreach ($_SESSION['basket'] as $item) {
if ($_item['product_id'] === [$_POST['id']]) {
$item['product_quantity'] += $_POST['quantity'];
}
}
}
}
?>
如果您需要更多详细信息,请告诉我。
更新
我写了另一个代码,但这次它只是将发送到购物篮的数量增加了一倍。我还在列出产品的页面上添加了一个隐藏的输入字段。
列出产品和将产品添加到购物车的页面上的代码:
<form> // Submitted via Ajax
<div>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"/>
<input type="hidden" name="do_it" value="quantity_again"/> // New input field added
<select name="quantity">
<?php
// Drop-down select quantity menu
for ($i=1; $i<=$row['stock']; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select>
</div>
<input type="hidden" name="hidden_photo" value="<?php echo $row['img_front_url']; ?>"/>
<input type="hidden" name="hidden_photo_alt" value="<?php echo $row['img_front_alt'];?>"/>
<input type="hidden" name="hidden_name" value="<?php echo $row['product']; ?>"/>
<input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/>
<button class="add-to-basket">Add to basket</button>
</form>
新代码仅将购物篮中的数量翻倍。
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
foreach ($_SESSION['basket'] as &$item) {
if ($item['product_id'] === $_POST['id']) {
$item['product_quantity'] += $_POST['quantity'];
}
}
}
}
看来$item['product_quantity']
和$_POST['quantity']
是一样的,所以数量翻倍了。
我想我需要先检查 $_POST['quantity']
是否存在于数组 $_SESSION['basket']
中,如果存在,将其存储在一个变量中然后执行 $item['product_quantity'] += $stored_qty
但这只是再次覆盖数量。这是代码:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'quantity_again') {
foreach ($_SESSION['basket'] as $item) {
if (in_array($_POST['quantity'], $_SESSION['basket'])) {
$stored_qty = $_POST['quantity'];
if ($item['product_id'] === $_POST['id']) {
$item['product_quantity'] += $stored_qty;
}
}
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['do_it']) && $_POST['do_it'] == 'change') {
if (isset($_SESSION['basket'])) {
$is_available = 0;
foreach ($_SESSION['basket'] as $keys => $values) {
if ($_SESSION['basket'][$keys]['product_id'] == $_POST['id']) {
$is_available++;
$_SESSION['basket'][$keys]['product_quantity'] += $_POST['quantity'];
}
}
}
if ($is_available == 0) {
if (!isset($_SESSION['basket'])) {
$_SESSION['basket'] = array();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
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'])
);
}
}
}
}
}
归功于 https://www.webslesson.info/2018/04/shopping-cart-by-using-bootstrap-popover-with-ajax-php.html
尽管如此,我不确定我是否理解 $is_available
在代码中的实际作用,以及它如何指代添加相同产品时增加的数量。