2012-08-31 6 views
1

장바구니 클래스를 생성 할 때 처음 장바구니에 제품을 추가 한 다음 다음에 장바구니에 제품을 추가 할 때 다음에 제품을 추가 할 수 없습니다.Codeigniter 장바구니

내가 원하는 걸 누군가가 장바구니에 항목을 추가하고 똑같은 항목이 이미 장바구니에있는 경우 장바구니의 수량이 증가합니다.

답변

4

당신이 카트 클래스에 보내는 몇 가지 코드 또는 어떤 데이터를 게시 할 수 있지만 올바른 방향을 가리켜 야하는 경우 그것은 도움이 될 것입니다 :

function add_cart_item(){ 

     $data = $_POST; 

     $id = $data['product_id']; //get new product id 
     $qty = $data['quantity'];  //get quantity if that item  
     $cart = $this->cart->contents(); //get all items in the cart 
      $exists = false;    //lets say that the new item we're adding is not in the cart 
      $rowid = ''; 

      foreach($cart as $item){ 
       if($item['id'] == $id)  //if the item we're adding is in cart add up those two quantities 
       { 
        $exists = true; 
        $rowid = $item['rowid']; 
        $qty = $item['qty'] + $qty; 
       }  
      } 

      if($exists) 
      { 
       $this->cart_model->update_item($rowid, $qty);     
       echo 'true'; // For ajax calls if javascript is enabled, return true, so the cart gets updated   
      } 
      else 
      { 
       if($this->cart_model->add_cart_item() == TRUE) 
       {   
        echo 'true'; // for ajax calls if javascript is enabled, return true, so the cart gets updated 
       } 
      } 

} 

과 cart_model 당신이 업데이트가 것이며, 이런 식으로 보이는 함수를 추가하십시오.

function update_item($rowid, $qty){  

     // Create an array with the products rowid's and quantities. 
     $data = array(
      'rowid' => $rowid, 
      'qty' => $qty 
     ); 

    // Update the cart with the new information 
     $this->cart->update($data); 
    } 


    // Add an item to the cart 
    function add_cart_item(){ 

     $id = $this->input->post('product_id'); // Assign posted product_id to $id 
     $qty = $this->input->post('quantity'); // Assign posted quantity to $cty 
     //$img = $this->input->post('image'); // Assign posted quantity to $img 

     $this->db->where('id', $id); // Select where id matches the posted id 
     $query = $this->db->get('products', 1); // Select the products where a match is found and limit the query by 1 

     // Check if a row has been found 
     if($query->num_rows > 0){ 

      foreach ($query->result() as $row) 
      { 
       $data = array(
        'id'  => $id, 
        'qty'  => $qty, 
        'price' => $row->price, 
        'name' => $row->name, 
        //'options' => array('image' => $img), 
       ); 

       $this->cart->insert($data); 

       return TRUE; 
      } 

     // Nothing found! Return FALSE! 
     }else{ 
      return FALSE; 
     } 
    } 
+0

감사합니다! 그것은 작동합니다. – user1189157