# 購物車類
購物車類允許項目被添加到 session 中,session 在用戶瀏覽你的網站期間都保持有效狀態。 這些項目能夠以標準的 "購物車" 格式被檢索和顯示,并允許用戶更新數量或者從購物車中移除項目。
**重要**
購物車類已經廢棄,請不要使用。目前保留它只是為了向前兼容。
請注意購物車類只提供核心的 "購物車" 功能。它不提供配送、信用卡授權或者其它處理組件。
# 使用購物車類
# 初始化購物車類
**重要**
購物車類利用 CodeIgniter 的 Session 類 把購物車信息保存到數據庫中, 所以在使用購物車類之前,你必須根據 Session 類文檔 中的說明來創建數據庫表, 并且在 application/config/config.php 文件中把 Session 相關參數設置為使用數據庫。
為了在你的控制器構造函數中初始化購物車類,請使用 $this->load->library 函數:
~~~
$this->load->library('cart');
~~~
一旦加載,就可以通過調用 $this->cart 來使用購物車對象了:
~~~
$this->cart
~~~
**注解**
購物車類會自動加載和初始化 Session 類,因此除非你在別處要用到 session,否則你不需要再次加載 Session 類。
# 將一個項目添加到購物車
要添加項目到購物車,只需將一個包含了商品信息的數組傳遞給 $this->cart->insert() 函數即可,就像下面這樣:
~~~
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
~~~
**重要**
上面的前四個數組索引(id、qty、price 和 name)是 必需的 。 如果缺少其中的任何一個,數據將不會被保存到購物車中。第5個索引(options) 是可選的。當你的商品包含一些相關的選項信息時,你就可以使用它。 正如上面所顯示的那樣,請使用一個數組來保存選項信息。
五個保留的索引分別是:
* id - 你的商店里的每件商品都必須有一個唯一的標識符。典型的標識符是庫存量單位(SKU)或者其它類似的標識符。
* qty - 購買的數量。
* price - 商品的價格。
* name - 商品的名稱。
* options - 標識商品的任何附加屬性。必須通過數組來傳遞。
除以上五個索引外,還有兩個保留字:rowid 和 subtotal。它們是購物車類內部使用的, 因此,往購物車中插入數據時,請不要使用這些詞作為索引。
你的數組可能包含附加的數據。你的數組中包含的所有數據都會被存儲到 session 中。 然而,最好的方式是標準化你所有商品的數據,這樣更方便你在表格中顯示它們。
~~~
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'coupon' => 'XMAS-50OFF'
);
$this->cart->insert($data);
~~~
如果成功的插入一條數據后,insert() 方法將會返回一個 id 值( $rowid )。
# 將多個項目添加到購物車
通過下面這種多維數組的方式,可以一次性添加多個產品到購物車中。 當你希望允許用戶選擇同一頁面中的多個項目時,這就非常有用了。
~~~
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
),
array(
'id' => 'sku_567ZYX',
'qty' => 1,
'price' => 9.95,
'name' => 'Coffee Mug'
),
array(
'id' => 'sku_965QRS',
'qty' => 1,
'price' => 29.95,
'name' => 'Shot Glass'
)
);
$this->cart->insert($data);
~~~
# 顯示購物車
為了顯示購物車的數據,你得創建一個 視圖文件,它的代碼類似于下面這個。
請注意這個范例使用了 表單輔助函數 。
~~~
<?php echo form_open('path/to/controller/update/method'); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('', 'Update your Cart'); ?></p>
~~~
# 更新購物車
為了更新購物車中的信息,你必須將一個包含了 Row ID 和數量的數組傳遞給 $this->cart->update() 函數。
**注解**
如果數量被設置為 0 ,那么購物車中對應的項目會被移除。
~~~
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
~~~
你也可以更新任何一個在新增購物車時定義的屬性,如:options、price 或其他用戶自定義字段。
~~~
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 1,
'price' => 49.95,
'coupon' => NULL
);
$this->cart->update($data);
~~~
# 什么是 Row ID?
當一個項目被添加到購物車時,程序所生成的那個唯一的標識符就是 row ID。 創建唯一 ID 的理由是,當購物車中相同的商品有不同的選項時,購物車就能夠對它們進行管理。
比如說,有人購買了兩件相同的 T-shirt (相同的商品ID),但是尺寸不同。 商品 ID (以及其它屬性)都會完全一樣,因為它們是相同的 T-shirt , 它們唯一的差別就是尺寸不同。因此購物車必須想辦法來區分它們, 這樣才能獨立地管理這兩件尺寸不同的 T-shirt 。而基于商品 ID 和其它相關選項信息來創建一個唯一的 "row ID" 就能解決這個問題。
在幾乎所有情況下,更新購物車都將是用戶通過 "查看購物車" 頁面來實現的,因此對開發者來說, 不必太擔心 "row ID" ,只要保證你的 "查看購物車" 頁面中的一個隱藏表單字段包含了這個信息, 并且確保它能被傳遞給表單提交時所調用的更新函數就行了。 請仔細分析上面的 "查看購物車" 頁面的結構以獲取更多信息。
# 類參考
**class CI_Cart**
**$product_id_rules** = '.a-z0-9_-'
用于驗證商品 ID 有效性的正則表達式規則,默認是:字母、數字、連字符(-)、下劃線(_)、句點(.)
* * * * *
**$product_name_rules** = 'w -.:'
用于驗證商品 ID 和商品名有效性的正則表達式規則,默認是:字母、數字、連字符(-)、下劃線(_)、冒號(:)、句點(.)
* * * * *
**$product_name_safe **= TRUE
是否只接受安全的商品名稱,默認為 TRUE 。
* * * * *
**insert**([$items = array()])
參數: $items (array) -- Items to insert into the cart
返回: TRUE on success, FALSE on failure
返回類型: bool
將項目添加到購物車并保存到 session 中,根據成功或失敗返回 TRUE 或 FALSE 。
* * * * *
**update**([$items = array()])
參數: $items (array) -- Items to update in the cart
返回: TRUE on success, FALSE on failure
返回類型: bool
該方法用于更新購物車中某個項目的屬性。一般情況下,它會在 "查看購物車" 頁面被調用, 例如用戶在下單之前修改商品數量。參數是個數組,數組的每一項必須包含 rowid 。
* * * * *
**remove**($rowid)
參數: $rowid (int) -- ID of the item to remove from the cart
返回: TRUE on success, FALSE on failure
返回類型: bool
根據 $rowid 從購物車中移除某個項目。
* * * * *
**total**()
返回: Total amount
返回類型: int
顯示購物車總額。
* * * * *
**total_items**()
返回: Total amount of items in the cart
返回類型: int
顯示購物車中商品數量。
* * * * *
**contents**([$newest_first = FALSE])
參數: $newest_first (bool) -- Whether to order the array with newest items first
返回: An array of cart contents
返回類型: array
返回一個數組,包含購物車的所有信息。參數為布爾值,用于控制數組的排序方式。 TRUE 為按購物車里的項目從新到舊排序,FALSE 為從舊到新。
* * * * *
**get_item**($row_id)
參數: $row_id (int) -- Row ID to retrieve
返回: Array of item data
返回類型: array
根據指定的 $rowid 返回購物車中該項的信息,如果不存在,返回 FALSE 。
* * * * *
**has_options**($row_id = '')
參數: $row_id (int) -- Row ID to inspect
返回: TRUE if options exist, FALSE otherwise
返回類型: bool
如果購物車的某項包含 options 則返回 TRUE 。該方法可以用在針對 contents() 方法的循環中, 你需要指定項目的 rowid ,正如上文 "顯示購物車" 的例子中那樣。
* * * * *
**product_options**([$row_id = ''])
參數: $row_id (int) -- Row ID
返回: Array of product options
返回類型: array
該方法返回購物車中某個商品的 options 數組。該方法可以用在針對 contents() 方法的循環中, 你需要指定項目的 rowid ,正如上文 "顯示購物車" 的例子中那樣。
* * * * *
**destroy**()
返回類型: void
清空購物車。該函數一般在用戶訂單處理完成之后調用。
- 空白目錄
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- 開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 向 CodeIgniter 貢獻你的力量
- 編寫 CodeIgniter 的文檔
- Developer's Certificate of Origin 1.1
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類