【发布时间】:2012-06-22 00:28:10
【问题描述】:
使用 CI 2.1.1 和原生 Cart 库
如果我多次插入一个项目(具有相同的产品 ID、相同的选项),它会替换而不是增加数量。
这可能是一个错误,我是否遗漏了什么,或者我自己添加此功能的最佳方法是什么?
【问题讨论】:
使用 CI 2.1.1 和原生 Cart 库
如果我多次插入一个项目(具有相同的产品 ID、相同的选项),它会替换而不是增加数量。
这可能是一个错误,我是否遗漏了什么,或者我自己添加此功能的最佳方法是什么?
【问题讨论】:
所以这是我的解决方案,在第 1 行更改 System/libraries/Cart.php。 233 到 244
可能有更好的方法来做到这一点,但它确实有效。我不明白为什么这个功能还没有
// EDIT: added check if idential rowid/item already in cart, then just increase qty
// without this addition, it would not increase qty but simply replace the item
if (array_key_exists($rowid, $this->_cart_contents))
{
$this->_cart_contents[$rowid]['qty'] += $items['qty'];
}
else
{
// let's unset this first, just to make sure our index contains only the data from this submission
unset($this->_cart_contents[$rowid]);
// Create a new index with our new row ID
$this->_cart_contents[$rowid]['rowid'] = $rowid;
// And add the new items to the cart array
foreach ($items as $key => $val)
{
$this->_cart_contents[$rowid][$key] = $val;
}
}
【讨论】:
这不是错误。这样看:你告诉 CI 你想要 1 个 productX 在你的购物车里。如果它已经存在,它将保持这种状态。 rowid 确实得到了更新。
编辑核心库不是一个好主意。这使您的应用程序依赖于您所做的更改,并且当您更新 CI 并忘记再次更改核心时,它可能会破坏它。
如果你真的希望能够在用户每次点击添加时增加qty 那么
我建议做一些类似于你所做的事情,但在你model。
检查产品是否已经在购物车中,获取qty 并将现有的qty 添加到新的。
这有意义吗?
【讨论】: