[eluser]greedyman[/eluser]
Please see my picture I atteached,
I'm only talk about insert function.
Insert many times is update cart, if I press insert cart button of (1) and press one time again (I have quantity is 2). And if I press insert cart button of (2) and press one time again (I have quantity is 2) ... etc ...
But if I press insert cart button of (1), (2), (3) ... and then (1). (1)'s quantity doesn't increase, it'll be insert in the last position of list with quantity is one. May be there is something wrong in my code?
Code:
public function add_to_cart() {
if ($this->cart->total_items() > 0) {
foreach($this->cart->contents() as $items) {
if ($_REQUEST["productName"] == $items['name']) {
$this->cart->update(
array(
'rowid' => $items['rowid'],
'qty' => $items['qty'] + 1)
);
}
else { // if product doesn't exit in list
$this->insert_cart();
}
}
}
else {// there is nothing in cart
$this->insert_cart();
}
redirect($_SERVER['HTTP_REFERER']);
}
public function insert_cart() {
$data = array(
'id' => $_REQUEST['productId'],
'name' => $_REQUEST['productName'],
'price' => $_REQUEST['productPrice'],
'qty' => 1,
'options' => array('img' => $_REQUEST['productImg'])
);
$this->cart->insert($data);
}