[英]PHP / Sessions Shopping Cart - Quantity in cart only updates for first item
正在修改我的添加到购物车功能的代码,如果购物车中已有产品,则无法更新数量时遇到了一些困难。 它只会为添加到购物车的第一个产品正确更新。
我的UPDATED添加到购物车代码是:
// -------------------------
// Adding item
// -------------------------
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
$itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["product_id"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
我在代码中缺少什么吗?
谢谢。 斯坦
编辑:更新的代码不起作用:
// -------------------------
// Adding item
// -------------------------
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
$itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["product_id"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
**foreach($_SESSION["cart_item"] as $k =>&$v){
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}**
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
火焰服:
我相信你这样做的时候
$_SESSION["cart_item"][$v]["quantity"] = $v++;
PHP实际上创建了您要遍历的数组的副本
由于您要写入此数组,因此应使用&
引用显式地进行迭代&
foreach($_SESSION["cart_item"] as $k =>&$v)...
我在这里尝试在会话$ pr_id =产品ID中存储数据
adding item to cart
if(isset($_SESSION['cart_items']))
{
//find index of an array if it exits then do not add else add to cart and then
if (!array_key_exists($pr_id,$_SESSION['cart_items']))
{
$_SESSION['cart_items'][$pr_id]['id'] = $pr_id;
$_SESSION['cart_items'][$pr_id]['quantity'] = $quantity;
}
}
else
{
$_SESSION['cart_items'][$pr_id]['id'] = $pr_id;
$_SESSION['cart_items'][$pr_id]['quantity'] = $quantity;
}
$arr = array();
$arr['count'] = count($_SESSION['cart_items']);
$arr['message'] = 'Item successFully added';
echo json_encode($arr);
这是工作代码,您可以在db中进行更新条目
用于更新产品的条目或数量
if (array_key_exists($pr_id,$_SESSION['cart_items']))
{
$_SESSION['cart_items'][$pr_id]['quantity']=$quantity;
}
$arr['count'] = count($_SESSION['cart_items']);
$arr['message'] = 'Quantity changed successfully. ';
$arr['status'] = true;
echo json_encode($arr);
我犯了和你一样的错误。 这就是我解决的方法:
不要使用product_id
。 product_id
是一个数字。 请使用数字和字母(例如product_name)的组合形式。 它为我工作。
$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_name"] . "'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.