Welcome Guest, Not a member yet? Register   Sign In
Wrong when update product shopping cart
#1

[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);
}
#2

[eluser]SmokeyJoe[/eluser]
seems that this $_REQUEST["productName"] is never equal to $items['name']

dump these two and see if there is a equal pair. or if maybe one of them is not an array...
#3

[eluser]greedyman[/eluser]
[quote author="SmokeyJoe" date="1374742128"]seems that this $_REQUEST["productName"] is never equal to $items['name']

dump these two and see if there is a equal pair. or if maybe one of them is not an array...
[/quote]

Thanks a lot! I've done it many days ago




Theme © iAndrew 2016 - Forum software by © MyBB