【发布时间】:2017-03-31 04:22:05
【问题描述】:
我正在开发一个 php 购物车,我正在尝试让购物车更新商品的数量,而不是为同一商品创建新条目。但是,当输入已经在购物车中的产品时,我的 foreach 语句仅根据第一个数组值检查它,然后为该产品创建一个新条目。
有人可以帮我解决这个问题并弄清楚为什么它没有检查整个数组列表吗?
这是我的更新方法:
function CheckForExistingEntry($id, $setOf, $quantity) {
// if the product ID and the SET OF is equal in multiple products, update the quanity instead of making new records
foreach ($_SESSION['shopping_cart'] as $key => $product) {
if ($id == $product['product_id'] && $setOf == $product['setOf']) {
// Update Cart Value
$_SESSION['shopping_cart'][$key]['quantity'] += $quantity;
$_SESSION['shopping_cart'][$key]['price'] *= $_SESSION['shopping_cart'][$key]['quantity'];
break;
} else {
// Add New Cart Value
AddToCart($id, $setOf, $quantity);
break;
}
}
}
【问题讨论】: