亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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

重要

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

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

  • 使用購物車類

    • 什么是行ID?

    • 初始化購物車類

    • 將商品添加到購物車

    • 將多個項目添加到購物車

    • 顯示購物車

    • 更新購物車

  • 類參考

使用購物車類

初始化購物車類

重要

Cart類使用CodeIgniter的會話類將購物車信息保存到數(shù)據(jù)庫,因此在使用Cart類之前,您必須按照會話文檔中的說明設(shè)置數(shù)據(jù)庫表,并在您的application / config / config.php中設(shè)置會話首選項文件來利用數(shù)據(jù)庫。

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

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

加載后,Cart對象將可用:

$this->cart

注意

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

將商品添加到購物車

要將商品添加到購物車,只需將包含商品信息的數(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);

重要

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

五個保留索引是:

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

  • 數(shù)量 - 正在購買的數(shù)量。

  • 價格 - 物品的價格。

  • 名稱 - 項目的名稱。

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

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

你的數(shù)組可能包含額外的數(shù)據(jù)。你在數(shù)組中包含的任何內(nèi)容都將存儲在會話中。但是,最好將所有產(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()如果您成功插入單個項目,該方法將返回$ rowid。

將多個項目添加到購物車

通過使用多維數(shù)組,如下所示,可以在一個動作中將多個產(chǎn)品添加到購物車。在希望允許用戶從同一頁面上的多個項目中進(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

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

請注意,這個例子使用了表單助手。

<?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和一個或多個預(yù)定義屬性的數(shù)組傳遞給$this->cart->update()方法。

注意

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

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

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

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

什么是行ID?

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

例如,假設(shè)有人購買兩個相同的T恤(相同的產(chǎn)品ID),但尺寸不同。產(chǎn)品ID(和其他屬性)對于兩種尺寸都是相同的,因為它們是同一件襯衫。唯一的區(qū)別是尺寸。因此,購物車必須具有識別這種差異的手段,以便兩種尺寸的襯衫可以獨立管理。它通過創(chuàng)建基于產(chǎn)品ID和與其關(guān)聯(lián)的任何選項的唯一“行ID”來實現(xiàn)。

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

類參考

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

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

$product_name_rules = 'w -.:'

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

$product_name_safe = TRUE

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

insert([$items = array()])

參數(shù):

$ items(array) - 要插入購物車的項目

返回:

成功為TRUE,失敗為FALSE

返回類型:

布爾

  • $ itemsarray) - 要插入購物車的項目

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) - 要在購物車中更新的項目

返回:

成功為TRUE,失敗為FALSE

返回類型:

布爾

  • $ itemsarray) - 要在購物車中更新的項目

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

remove($rowid)

參數(shù):

$ rowid(int) - 要從購物車中移除的物品的ID

返回:

成功為TRUE,失敗為FALSE

返回類型:

布爾

  • $ rowidint) - 要從購物車中移除的物品的ID

Returns:  TRUE on success, FALSE on failure
Return type:  bool
允許您通過傳遞`$ rowid`來從購物車中刪除商品。

total()

返回:

總金額

返回類型:

INT

total_items()

返回:

購物車中的物品總量

返回類型:

INT

contents([$newest_first = FALSE])

參數(shù):

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

返回:

購物車內(nèi)容的數(shù)組

返回類型:

排列

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

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

get_item($row_id)

參數(shù):

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

返回:

項目數(shù)據(jù)的數(shù)組

返回類型:

排列

  • $ row_idint) - 要檢索的行ID

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

has_options($row_id = '')

參數(shù):

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

返回:

如果選項存在則為TRUE,否則為FALSE

返回類型:

布爾

  • $ row_idint) - 要檢查的行ID

Returns:  TRUE if options exist, FALSE otherwise
Return type:  bool
如果購物車中的特定行包含選項,則返回TRUE(布爾值)。這個方法被設(shè)計成在`content()`循環(huán)中使用,因為你必須將rowid傳遞給這個方法,如上面的顯示購物車示例所示。

product_options([$row_id = ''])

參數(shù):

$ row_id(int) - 行ID

返回:

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

返回類型:

排列

  • $row_id (int) – Row ID

返回:產(chǎn)品選項數(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: