有没有办法检查CI图表中是否存在ID,只更新该产品的数量,如果没有添加新项目?
这是我到目前为止所做的,但没有任何工作:(
if (isset($_POST['sendorder']))
{
$qty=$_POST['productquantity'];
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $heading
);
if (count($this->cart->contents())>0){
foreach ($this->cart->contents() as $item){
if ($item['id']==$id){
$data = array('rowid'=>$item['rowid'],
'qty'=>++$item['qty']);
$this->cart->update($data);
}
else{
$this->cart->insert($data);
}
}
}
}
答案 0 :(得分:1)
试试这个
// set $flag default value to true for inserting item to the cart session
$insert_new = TRUE;
$bag = $this->cart->contents();
foreach ($bag as $item) {
// check product id in session, if exist update the quantity
if ( $item['id'] == '1' ) { // Set value to your variable
$data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
$this->cart->update($data);
// set $insert_new value to False
$insert_new = FALSE;
}
}
// if $insert_new value is true, insert the item as new item in the cart
if ($insert_new) {
$data = array(
'id' => 1,
'qty' => 1,
'price' => 40,
'name' => 'Product item'
);
$this->cart->insert($data);
}