[英]Incrementing quantity of items in cart when item is added again
When I add a particular item into the cart if the item is already in the cart, then I need to update its quantity. 如果该商品已经在购物车中,则当我向购物车中添加特定商品时,我需要更新其数量。 Right now a duplicate item is added instead of updating the quantity.
现在,添加了重复项目,而不是更新数量。 Can anyone help please!
任何人都可以帮忙! Code below.
下面的代码。
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM medicine WHERE med_id='" . $_GET["med_id"] . "'");
$itemArray = array(
$productByCode[0]["med_id"]=>array(
'name'=>$productByCode[0]["med_name"],
'med_id'=>$productByCode[0]["med_id"],
'image'=>$productByCode[0]["Image"],
'quantity'=>$_POST["quantity"],
'price'=>$productByCode[0]["unit_cost"]
)
);
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["med_id"],$_SESSION["cart_item"])){
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["med_id"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
}
else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
}
else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v){
if($_GET["med_id"] == $_SESSION["cart_item"][$k]['med_id'])
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
To address the adding issues, I would maybe see if reworking the logic by using a function would help. 为了解决增加的问题,我可能会看看通过使用函数来重做逻辑是否会有所帮助。 I would also clean it up a bit.
我也要清理一下。 Store the functions on separate page(s) and include them before use:
将功能存储在单独的页面上,并在使用之前包括它们:
function getMedsById($id,$db)
{
if(!is_numeric($id))
return false;
$productByCode = $db->runQuery("SELECT * FROM medicine WHERE med_id='" . $id . "'");
$product = (!empty($productByCode[0]["med_id"]))? $productByCode[0] : false;
if(empty($product))
return false;
return array(
'name'=>$product["med_name"],
'med_id'=>$product["med_id"],
'image'=>$product["Image"],
'price'=>$product["unit_cost"]
);
}
function addToCart($array,$id,$qty)
{
$array['quantity'] = $qty;
# Save item to cart
$_SESSION["cart_item"][$id] = $array;
}
Rework of add in switch: 返修开关:
case "add":
if(!empty($_POST["quantity"])) {
# Just do a basic ternary to avoid warnings
$id = (!empty($_GET["med_id"]))? $_GET["med_id"] : false;
# Check input not manipulated
$qty = (!empty($_POST['quantity']) && is_numeric($_POST['quantity']))? $_POST['quantity'] : 1;
# Use the function to get the item from db
$prod = getMedsById($id,$db_handle);
# Stop if return is empty (user may be trying to manipulate cart)
if(empty($prod))
break;
# Same as yours
if(!empty($_SESSION["cart_item"])) {
# Check if this product is already set
if(isset($_SESSION["cart_item"][$id])){
# If already set, just add the qty to existing
$_SESSION['cart_item'][$id]['quantity'] += $qty;
# Stop the add process here
break;
}
}
# Just add to cart here, it won't get here if already in cart
addToCart($prod,$id,$qty);
}
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.