[英]Update multiple products in shopping cart
我正在使用以下代碼更新購物車,但是此代碼正在更新單個產品的購物車。 如何更新多個產品的購物車?
update.php (此頁面用於輸入文本框,即數量)
<?php
session_start();
?>
<html>
<body>
<form action="update1.php" method="post">
Quantity:
<input type="text" name="qty" value="">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
update1.php (此代碼用於更新購物車的數量)
// foreach ( $value as $key=> $final_val ){
foreach($value as $product)
{
if($_REQUEST['pro_id'] == $product['pro_id'])
{
$found = true;
break;
}
}
if($found)
{
$_SESSION['cart'][$_REQUEST['pro_id']]['qty'] ;
$_SESSION[$x]=$product['qty'];
$_SESSION["$qty"] = $_SESSION[$x]+$qty1;
echo "qty:".$_SESSION["$qty"];
}
else
{
// go get new product and add to $_SESSION['cart']
echo"not done";
}
//}
echo "<h4 align='center'> click here to <a href='shoppingcart.php'>see list</a> </h4>";
?>
我仍然不確定100%的意思,因此,我將以一種形式簡單地演示倍數。 您的購物車可能沒有類別或功能,因此出於演示目的,我做了一個非常簡單的說明。 您需要將輸入排列在表單上。
/classes/ShoppingCart.php
<?php
class ShoppingCart
{
public function initialize()
{
if(!isset($_SESSION['cart']))
$_SESSION['cart'] = array();
}
public function addToCart($id,$qty = 1)
{
if(empty($_SESSION['cart'][$id]))
$_SESSION['cart'][$id] = $qty;
else
$_SESSION['cart'][$id] += $qty;
}
public function resetItem($id,$qty = 1)
{
$_SESSION['cart'][$id] = $qty;
}
public function removeItem($id,$qty = false)
{
if(!$qty || !is_numeric($qty)) {
if(isset($_SESSION['cart'][$id]))
unset($_SESSION['cart'][$id]);
}
else {
if(empty($_SESSION['cart'][$id]))
return false;
$amt = ($_SESSION['cart'][$id] - $qty);
if($amt <= 0)
unset($_SESSION['cart'][$id]);
else
$_SESSION['cart'][$id] = $amt;
}
}
public function getItem($id)
{
return (!empty($_SESSION['cart'][$id]))? $_SESSION['cart'][$id] : 0;
}
public function clearCart()
{
if(isset($_SESSION['cart']))
unset($_SESSION['cart']);
}
}
/update.php
<?php
session_start();
// Using spl_autoload_register() would be better than manual include
require_once(__DIR__.'/classes/ShoppingCart.php');
// Start the cart
$cartEngine = new ShoppingCart();
?>
<html>
<body>
<form action="" method="post">
<input type="hidden" name="action" value="update" />
<ul>
<li>
<h4>Product 1</h4>
QTY<input type="text" name="item[1][qty]" value="<?php echo $cartEngine->getItem('ITM1'); ?>" />
<input type="hidden" name="item[1][prod_id]" value="ITM1" />
</li>
<li>
<h4>Product 2</h4>
QTY<input type="text" name="item[2][qty]" value="<?php echo $cartEngine->getItem('ITM2'); ?>" />
<input type="hidden" name="item[2][prod_id]" value="ITM2" />
</li>
<li>
<h4>Product 3</h4>
QTY<input type="text" name="item[3][qty]" value="<?php echo $cartEngine->getItem('ITM3'); ?>" />
<input type="hidden" name="item[3][prod_id]" value="ITM3" />
</li>
</ul>
<input type="submit" name="submit" value="Submit">
</form>
<form action="" method="post">
<input type="hidden" name="action" value="clear" />
<input type="submit" value="CLEAR CART" />
</form>
</body>
</html>
這為您提供了一個提交數組:
Array
(
[action] => update_cart
[item] => Array
(
[1] => Array
(
[qty] => 1
[prod_id] => ITM1
)
[2] => Array
(
[qty] => 2
[prod_id] => ITM2
)
[3] => Array
(
[qty] => 1
[prod_id] => ITM3
)
)
[submit] => Submit
)
除非您正在執行ajax,否則我個人將不會有一個全新的頁面進行處理。 我會將這一部分放在頁面頂部,然后重新加載同一頁面,但這就是我。 要處理數組,將類似於:
// Using spl_autoload_register() would be better than manual include
require_once(__DIR__.'/classes/ShoppingCart.php');
// Start the cart
$cartEngine = new ShoppingCart();
// See if action happens
if(!empty($_POST['action'])) {
// Start the cart
$cartEngine->initialize();
// Do the cart stuff
switch($_POST['action']) {
case('update'):
foreach($_POST['item'] as $item) {
$cartEngine->resetItem($item['prod_id'],$item['qty']);
}
break;
case('clear'):
$cartEngine->clearCart();
break;
}
}
購物車會很簡單(如果您想存儲更多的數據而不是數量和物品代碼,則可能會變得更加復雜),但這只是為了演示,它會輸出一個簡單的數組,例如:
[cart] => Array
(
[ITM1] => 1
[ITM2] => 2
[ITM3] => 4
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.