国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

directory search
Array Array Helper Benchmarking Benchmarking Class Caching Caching Driver Calendaring Calendaring Class CAPTCHA CAPTCHA Helper Config Config Class Cookie Cookie Helper Database Connecting to your Database Custom Function Calls Database Caching Class Database Configuration Database Forge Class Database Metadata Database Quick Start: Example Code Database Reference Database Utility Class DB Driver Reference Generating Query Results Queries Query Builder Class Query Helper Methods Transactions Date Date Helper Directory Directory Helper Download Download Helper Email Email Class Email Helper Encrypt Encrypt Class Encryption Encryption Library File File Helper File Uploading File Uploading Class Form Form Helper Form Validation Form Validation FTP FTP Class Functions compatibility_functions common_functions HTML HTML Helper HTML Table HTML Table Class Image Manipulation Image Manipulation Class Inflector Inflector Helper Input Input Class Javascript Javascript Class Language Language Class Language Helper Loader Loader Class Migrations Migrations Class Number Number Helper Output Output Class Pagination Pagination Class Path Path Helper Security Security Class Security Helper Session Session Library Shopping Cart Shopping Cart Class Smiley Smiley Helper String String Helper Template Parser Template Parser Class Text Text Helper Trackback Trackback Class Typography Typography Class Typography Helper Unit Testing Unit Testing Class URI URL User Agent XML XML-RPC and XML-RPC Server Zip Encoding Zip Encoding Class XML-RPC and XML-RPC Server Classes XML Helper User Agent Class URL Helper URI Class
characters

購(gòu)物車(chē)類允許將項(xiàng)目添加到用戶瀏覽您的網(wǎng)站時(shí)保持活動(dòng)的會(huì)話。這些物品可以以標(biāo)準(zhǔn)的“購(gòu)物車(chē)”格式檢索和顯示,允許用戶更新數(shù)量或從購(gòu)物車(chē)中移除物品。

重要

購(gòu)物車(chē)圖書(shū)館已被拒絕,不應(yīng)使用。目前僅保留用于向后兼容。

請(qǐng)注意,購(gòu)物車(chē)類僅提供核心“購(gòu)物車(chē)”功能。它不提供運(yùn)輸,信用卡授權(quán)或其他處理組件。

  • 使用購(gòu)物車(chē)類

    • 什么是行ID?

    • 初始化購(gòu)物車(chē)類

    • 將商品添加到購(gòu)物車(chē)

    • 將多個(gè)項(xiàng)目添加到購(gòu)物車(chē)

    • 顯示購(gòu)物車(chē)

    • 更新購(gòu)物車(chē)

  • 類參考

使用購(gòu)物車(chē)類

初始化購(gòu)物車(chē)類

重要

Cart類使用CodeIgniter的會(huì)話類將購(gòu)物車(chē)信息保存到數(shù)據(jù)庫(kù),因此在使用Cart類之前,您必須按照會(huì)話文檔中的說(shuō)明設(shè)置數(shù)據(jù)庫(kù)表,并在您的application / config / config.php中設(shè)置會(huì)話首選項(xiàng)文件來(lái)利用數(shù)據(jù)庫(kù)。

要在控制器構(gòu)造函數(shù)中初始化購(gòu)物車(chē)類,請(qǐng)使用以下$this->load->library()方法:

$this->load->library('cart');

加載后,Cart對(duì)象將可用:

$this->cart

注意

Cart類將自動(dòng)加載和初始化會(huì)話類,因此除非您在應(yīng)用程序的其他地方使用會(huì)話,否則不需要加載Session類。

將商品添加到購(gòu)物車(chē)

要將商品添加到購(gòu)物車(chē),只需將包含商品信息的數(shù)組傳遞給$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);

重要

上面的前四個(gè)數(shù)組索引(id,qty,price和name)是必需的。如果您忽略其中的任何一項(xiàng),則數(shù)據(jù)將不會(huì)保存到購(gòu)物車(chē)。第五個(gè)索引(選項(xiàng))是可選的。它旨在用于您的產(chǎn)品具有與之相關(guān)聯(lián)的選項(xiàng)的情況。如上所示,使用數(shù)組作為選項(xiàng)。

五個(gè)保留索引是:

  • id - 商店中的每個(gè)產(chǎn)品都必須具有唯一標(biāo)識(shí)符。通常這將是一個(gè)“SKU”或其他這樣的標(biāo)識(shí)符。

  • 數(shù)量 - 正在購(gòu)買(mǎi)的數(shù)量。

  • 價(jià)格 - 物品的價(jià)格。

  • 名稱 - 項(xiàng)目的名稱。

  • 選項(xiàng) - 識(shí)別產(chǎn)品所需的任何其他屬性。這些必須通過(guò)數(shù)組傳遞。

除上述五個(gè)索引外,還有兩個(gè)保留字:rowid和小計(jì)。這些由Cart類內(nèi)部使用,因此請(qǐng)勿在將數(shù)據(jù)插入購(gòu)物車(chē)時(shí)將這些詞用作索引名稱。

你的數(shù)組可能包含額外的數(shù)據(jù)。你在數(shù)組中包含的任何內(nèi)容都將存儲(chǔ)在會(huì)話中。但是,最好將所有產(chǎn)品中的數(shù)據(jù)標(biāo)準(zhǔn)化,以便使表格中的信息更容易顯示。

$data = array(        'id'      => 'sku_123ABC',        'qty'     => 1,        'price'   => 39.95,        'name'    => 'T-Shirt',        'coupon'         => 'XMAS-50OFF');$this->cart->insert($data);

insert()如果您成功插入單個(gè)項(xiàng)目,該方法將返回$ rowid。

將多個(gè)項(xiàng)目添加到購(gòu)物車(chē)

通過(guò)使用多維數(shù)組,如下所示,可以在一個(gè)動(dòng)作中將多個(gè)產(chǎn)品添加到購(gòu)物車(chē)。在希望允許用戶從同一頁(yè)面上的多個(gè)項(xiàng)目中進(jìn)行選擇的情況下,這非常有用。

$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);

顯示cart

要顯示購(gòu)cart,您將創(chuàng)建一個(gè)代碼類似于下面顯示的代碼的視圖文件。

請(qǐng)注意,這個(gè)例子使用了表單助手。

<?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>

更新cart

要更新cart中的信息,您必須將包含行ID和一個(gè)或多個(gè)預(yù)定義屬性的數(shù)組傳遞給$this->cart->update()方法。

注意

如果數(shù)量設(shè)置為零,則該物品將從購(gòu)物車(chē)中移除。

$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);

您也可以在插入項(xiàng)目時(shí)更新您之前定義的任何屬性,例如選項(xiàng),價(jià)格或其他自定義字段。

$data = array(        'rowid'  => 'b99ccdf16028f015540f341130b6d8ec',        'qty'    => 1,        'price'  => 49.95,        'coupon' => NULL);$this->cart->update($data);

什么是行ID?

行ID是在將商品添加到購(gòu)物車(chē)時(shí)由購(gòu)物車(chē)代碼生成的唯一標(biāo)識(shí)符。創(chuàng)建唯一ID的原因是,可以通過(guò)購(gòu)物車(chē)管理具有不同選項(xiàng)的相同產(chǎn)品。

例如,假設(shè)有人購(gòu)買(mǎi)兩個(gè)相同的T恤(相同的產(chǎn)品ID),但尺寸不同。產(chǎn)品ID(和其他屬性)對(duì)于兩種尺寸都是相同的,因?yàn)樗鼈兪峭患r衫。唯一的區(qū)別是尺寸。因此,購(gòu)物車(chē)必須具有識(shí)別這種差異的手段,以便兩種尺寸的襯衫可以獨(dú)立管理。它通過(guò)創(chuàng)建基于產(chǎn)品ID和與其關(guān)聯(lián)的任何選項(xiàng)的唯一“行ID”來(lái)實(shí)現(xiàn)。

在幾乎所有情況下,更新購(gòu)物車(chē)都是用戶通過(guò)“查看購(gòu)物車(chē)”頁(yè)面所做的事情,因此對(duì)于開(kāi)發(fā)者來(lái)說(shuō),您不可能永遠(yuǎn)不必關(guān)心“行ID”,除非確保您的“查看購(gòu)物車(chē)”頁(yè)面在隱藏表單域中包含此信息,并確保update()在提交更新表單時(shí)將其傳遞給方法。請(qǐng)檢查上面的“查看購(gòu)物車(chē)”頁(yè)面的結(jié)構(gòu)以獲取更多信息。

類參考

class CI_Cart$product_id_rules = '.a-z0-9_-'

這些是我們用來(lái)驗(yàn)證產(chǎn)品ID的正則表達(dá)式規(guī)則 - 默認(rèn)情況下是字母數(shù)字,短劃線,下劃線或句點(diǎn)

$product_name_rules = 'w -.:'

這些是我們用來(lái)驗(yàn)證產(chǎn)品ID和產(chǎn)品名稱的正則表達(dá)式規(guī)則 - 默認(rèn)情況下是字母數(shù)字,破折號(hào),下劃線,冒號(hào)或句點(diǎn)

$product_name_safe = TRUE

是否僅允許安全的產(chǎn)品名稱。默認(rèn)為T(mén)RUE。

insert([$items = array()])

參數(shù):

$ items(array) - 要插入購(gòu)物車(chē)的項(xiàng)目

返回:

成功為T(mén)RUE,失敗為FALSE

返回類型:

布爾

  • $ itemsarray) - 要插入購(gòu)物車(chē)的項(xiàng)目

Returns:  TRUE on success, FALSE on failure
Return type:  bool
Insert items into the cart and save it to the session table. Returns TRUE on success and FALSE on failure.

update([$items = array()])

參數(shù):

$ items(array) - 要在購(gòu)物車(chē)中更新的項(xiàng)目

返回:

成功為T(mén)RUE,失敗為FALSE

返回類型:

布爾

  • $ itemsarray) - 要在購(gòu)物車(chē)中更新的項(xiàng)目

Returns:  TRUE on success, FALSE on failure
Return type:  bool
此方法允許更改給定項(xiàng)目的屬性。通常情況下,如果用戶在結(jié)帳前更改數(shù)量,則會(huì)從“查看購(gòu)物車(chē)”頁(yè)面中調(diào)用。該數(shù)組必須包含每個(gè)項(xiàng)目的rowid。

remove($rowid)

參數(shù):

$ rowid(int) - 要從購(gòu)物車(chē)中移除的物品的ID

返回:

成功為T(mén)RUE,失敗為FALSE

返回類型:

布爾

  • $ rowidint) - 要從購(gòu)物車(chē)中移除的物品的ID

Returns:  TRUE on success, FALSE on failure
Return type:  bool
允許您通過(guò)傳遞`$ rowid`來(lái)從購(gòu)物車(chē)中刪除商品。

total()

返回:

總金額

返回類型:

INT

total_items()

返回:

購(gòu)物車(chē)中的物品總量

返回類型:

INT

contents([$newest_first = FALSE])

參數(shù):

$ newest_first(bool) - 是否先用最新的項(xiàng)目排列數(shù)組

返回:

購(gòu)物車(chē)內(nèi)容的數(shù)組

返回類型:

排列

  • $ newest_firstbool) - 是否先用最新的項(xiàng)目排列數(shù)組

Returns:  An array of cart contents
Return type:  array
返回包含購(gòu)物車(chē)中所有內(nèi)容的數(shù)組。您可以通過(guò)傳遞數(shù)組來(lái)排序返回?cái)?shù)組的順序TRUE將內(nèi)容從最新到最舊排序,否則將從最舊到最新排序。

get_item($row_id)

參數(shù):

$ row_id(int) - 要檢索的行ID

返回:

項(xiàng)目數(shù)據(jù)的數(shù)組

返回類型:

排列

  • $ row_idint) - 要檢索的行ID

Returns:  Array of item data
Return type:  array
返回包含與指定行ID匹配的項(xiàng)的數(shù)據(jù)的數(shù)組,如果不存在此類項(xiàng),則返回FALSE。

has_options($row_id = '')

參數(shù):

$ row_id(int) - 要檢查的行ID

返回:

如果選項(xiàng)存在則為T(mén)RUE,否則為FALSE

返回類型:

布爾

  • $ row_idint) - 要檢查的行ID

Returns:  TRUE if options exist, FALSE otherwise
Return type:  bool
如果購(gòu)物車(chē)中的特定行包含選項(xiàng),則返回TRUE(布爾值)。這個(gè)方法被設(shè)計(jì)成在`content()`循環(huán)中使用,因?yàn)槟惚仨殞owid傳遞給這個(gè)方法,如上面的顯示購(gòu)物車(chē)示例所示。

product_options([$row_id = ''])

參數(shù):

$ row_id(int) - 行ID

返回:

一系列產(chǎn)品選項(xiàng)

返回類型:

排列

  • $row_id (int) – Row ID

返回:產(chǎn)品選項(xiàng)數(shù)組
Return type:  array
Returns an array of options for a particular product. This method is designed to be used in a loop with `contents()`, since you must pass the rowid to this method, as shown in the Displaying the Cart example above.

destroy()

Return type:

void

Previous article: Next article: