2011-06-16 56 views
0

如果產品已經存在於購物車中,如何更新商品數量?如果產品存在,Codeigniter更新購物車

這裏是我的添加到購物車功能:

public function add() { 
     $product= $this->products_model->listProduct($this->input->post('id_product')); 

     $data = array(
      'id' => $this->input->post('id_product'), 
      'qty' => $this->input->post('qty'), 
      'price' => $product->price, 
      'name' => $product->title   ); 

     $this->cart->insert($data); 
     redirect('product/'.$product->id.'/'.url_title($product->title, 'dash', TRUE)); 
    } 

有了這個功能,如果產品在購物車中存在,讓我們用8量說,如果我的1數量增加同樣的產品,它會是1而不是9.

在此先感謝。

回答

0

我看不出$produto是在哪裏定義的,應該不是,而是分別爲$product->price$product->title

按照docs

重要:前四個陣列 索引上述(ID,數量,價格,和 名)是必需的。如果您省略了 中的任何一個,則數據將不會保存到 購物車。

一定要檢查他們是否已經設置好了(也許看看拼寫錯誤,比如我在黑暗中拍攝'qtd'而不是'qty'等)。 此外,如果要更新產品已經存在,你需要使用$this->cart->update()而不是$ this->cart->insert()

+0

嗨達米安皮爾西,感謝您的答覆。這實際上是一個錯字。我粘貼了這些代碼,並更改了一些變量名稱,以便在英語中更有意義,因爲它們是用其他語言編寫的。看起來有些保持不變。但功能實際上起作用。我需要知道的是如何檢查產品是否存在於購物車中。如果沒有,我運行'$ this-> cart-> insert()'如果它運行'$ this-> cart-> update()'。但是我想把所有的東西放在函數add()中,這樣當產品被添加到購物車時,我總是運行相同的函數。謝謝。 – AFRC 2011-06-17 08:02:01

1

@AFRC 你可以嘗試

public function add() { 
     $flag = TRUE; 
     $dataTmp = $this->cart->contents(); 

     foreach ($dataTmp as $item) { 
      if ($item['id'] == 'sku_123ABC') { 
       echo "Found Id so updatig quantity"; 
       $qtyn = $item['qty'] + 1; 
       $this->update($item['rowid'], $qtyn); 
       $flag = FALSE; 
       break; 
      } 
     } 
     if ($flag) { 
      $data = array(
       'id' => 'sku_123ABC', 
       'qty' => 1, 
       'price' => 39.95, 
       'name' => 'T-Shirt', 
      ); 
      $this->cart->insert($data); 
      echo "Inserting as not found in the cart"; 
     } 
     echo "Add1 called"; 
    } 
0

只是這個檢查,如果你有選擇

$rowid == md5($items['id'].implode('', $items['options'])); 

如果不是

$rowid == md5($items['id']); 
0

使用此代碼添加到購物車

if (count($this->cart->contents())>0){ 
        foreach ($this->cart->contents() as $item){ 
         if ($item['id']==$product->id){ 
          $data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']); 
          $this->cart->update($data); 
         }else{ 
          $data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title)); 
          $this->cart->insert($data); 
         } 
        } 
0

我用AFRC的回答下面創建的代碼,它可能會有所幫助。感謝您的回覆,它真的幫助了我!如果您的購物車更新中會出現與您的購物車相匹配的ID,您可以在下面調用購物車功能,否則請插入。它還會顯示購物車視圖,不管是插入,更新還是僅顯示。請注意,我有一個單獨的函數get_specific_craft用於獲取slu items物品數據(例如價格和名稱)。

public function cart($slug = "") 
{ 
    if($slug != "") 
    { 
     $craft = $this->crafts_model->get_specific_craft($slug); 

     $flag = TRUE; 

     foreach ($this->cart->contents() as $item) 
     { 

      if ($item['id'] == $slug) 
      { 
       $qtyn = $item['qty'] + 1; 

       $dat = array(
        'rowid' => $item['rowid'], 
        'qty' => $qtyn 
       ); 


       $this->cart->update($dat); 
       $flag = FALSE; 
       break; 
      } 
     } 

     if ($flag) 
     { 
      $crt = array(
       'id'  => $craft['id'], 
       'qty'  => 1, 
       'price' => $craft['price'], 
       'name' => $craft['name'] 
       //'options' => array('Size' => 'L', 'Color' => 'Red') 
      ); 
      $this->cart->insert($crt); 

     }  
    } 
    $this->load->view('cart' , $this->data); 

} 
相關問題