346 lines
18 KiB
HTML
346 lines
18 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<title>Shopping Cart Class : CodeIgniter User Guide</title>
|
||
|
|
||
|
<style type='text/css' media='all'>@import url('../userguide.css');</style>
|
||
|
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
||
|
|
||
|
<script type="text/javascript" src="../nav/nav.js"></script>
|
||
|
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
||
|
<script type="text/javascript" src="../nav/moo.fx.js"></script>
|
||
|
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
||
|
|
||
|
<meta http-equiv='expires' content='-1' />
|
||
|
<meta http-equiv= 'pragma' content='no-cache' />
|
||
|
<meta name='robots' content='all' />
|
||
|
<meta name='author' content='ExpressionEngine Dev Team' />
|
||
|
<meta name='description' content='CodeIgniter User Guide' />
|
||
|
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<!-- START NAVIGATION -->
|
||
|
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
||
|
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
||
|
<div id="masthead">
|
||
|
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
||
|
<tr>
|
||
|
<td><h1>CodeIgniter User Guide Version 1.7</h1></td>
|
||
|
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
<!-- END NAVIGATION -->
|
||
|
|
||
|
|
||
|
<!-- START BREADCRUMB -->
|
||
|
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
||
|
<tr>
|
||
|
<td id="breadcrumb">
|
||
|
<a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
||
|
<a href="../index.html">User Guide Home</a> ›
|
||
|
Shopping Cart Class
|
||
|
</td>
|
||
|
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
<!-- END BREADCRUMB -->
|
||
|
|
||
|
<br clear="all" />
|
||
|
|
||
|
|
||
|
<!-- START CONTENT -->
|
||
|
<div id="content">
|
||
|
|
||
|
|
||
|
<h1>Shopping Cart Class</h1>
|
||
|
|
||
|
<p>The Cart Class permits items to be added to a session that stays active while a user is browsing your site.
|
||
|
These items can be retrieved and displayed in a standard "shopping cart" format, allowing the user to update the quantity or remove items from the cart.</p>
|
||
|
|
||
|
<p>Please note that the Cart Class ONLY provides the core "cart" functionality. It does not provide shipping, credit card authorization, or other processing components.</p>
|
||
|
|
||
|
|
||
|
<h2>Initializing the Shopping Cart Class</h2>
|
||
|
|
||
|
<p><strong>Important:</strong> The Cart class utilizes CodeIgniter's
|
||
|
<a href="sessions.html">Session Class</a> to save the cart information to a database, so before using the Cart class you must set up a database table
|
||
|
as indicated in the <a href="sessions.html">Session Documentation</a> , and set the session preferences in your <kbd>appliction/config/config.php</kbd> file to utilize a database.</p>
|
||
|
|
||
|
<p>To initialize the Shopping Cart Class in your controller constructor, use the <dfn>$this->load->library</dfn> function:</p>
|
||
|
|
||
|
<code>$this->load->library('cart');</code>
|
||
|
<p>Once loaded, the Cart object will be available using: <dfn>$this->cart</dfn></p>
|
||
|
|
||
|
<p class="important"><strong>Note:</strong> The Cart Class will load and initialize the Session Class automatically, so unless you are using sessions elsewhere in your application, you do not need to load the Session class.</p>
|
||
|
|
||
|
<h2>Adding an Item to The Cart</h2>
|
||
|
|
||
|
<p>To add an item to the shopping cart, simply pass an array with the product information to the <dfn>$this->cart->insert()</dfn> function, as shown below:</p>
|
||
|
|
||
|
<code>
|
||
|
$data = array(<br />
|
||
|
'id' => 'sku_123ABC',<br />
|
||
|
'qty' => 1,<br />
|
||
|
'price' => 39.95,<br />
|
||
|
'name' => 'T-Shirt',<br />
|
||
|
'options' => array('Size' => 'L', 'Color' => 'Red')<br />
|
||
|
);<br />
|
||
|
<br />
|
||
|
|
||
|
$this->cart->insert($data);
|
||
|
|
||
|
</code>
|
||
|
|
||
|
<p class="important"><strong>Important:</strong> The first four array indexes above (<dfn>id</dfn>, <dfn>qty</dfn>, <dfn>price</dfn>, and <dfn>name</dfn>) are <strong>required</strong>.
|
||
|
If you omit any of them the data will not be saved to the cart. The fifth index (<dfn>options</dfn>) is optional.
|
||
|
It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.</p>
|
||
|
|
||
|
<p>The five reserved indexes are:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li><strong>id</strong> - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.</li>
|
||
|
<li><strong>qty</strong> - The quantity being purchased.
|
||
|
<li><strong>price</strong> - The price of the item.
|
||
|
<li><strong>name</strong> - The name of the item.
|
||
|
<li><strong>options</strong> - Any additional attributes that are needed to identify the product. These must be passed via an array.
|
||
|
</ul>
|
||
|
|
||
|
<p>In addition to the five indexes above, there are two reserved words: <dfn>rowid</dfn> and <dfn>subtotal</dfn>. These are used internally by the Cart class, so
|
||
|
please do NOT use those words as index names when inserting data into the cart.</p>
|
||
|
|
||
|
<p>Your array may contain additional data. Anything you include in your array will be stored in the session. However, it is best to standardize your data among
|
||
|
all your products in order to make displaying the information in a table easier.</p>
|
||
|
|
||
|
|
||
|
<h2>Adding Multiple Items to The Cart</h2>
|
||
|
|
||
|
<p>By using a multi-dimensional array, as shown below, it is possible to add multiple products to the cart in one action. This is useful in cases where you wish to allow
|
||
|
people to select from among several items on the same page.</p>
|
||
|
|
||
|
|
||
|
<code>
|
||
|
$data = array(<br />
|
||
|
|
||
|
array(<br />
|
||
|
'id' => 'sku_123ABC',<br />
|
||
|
'qty' => 1,<br />
|
||
|
'price' => 39.95,<br />
|
||
|
'name' => 'T-Shirt',<br />
|
||
|
'options' => array('Size' => 'L', 'Color' => 'Red')<br />
|
||
|
),<br />
|
||
|
|
||
|
array(<br />
|
||
|
'id' => 'sku_567ZYX',<br />
|
||
|
'qty' => 1,<br />
|
||
|
'price' => 9.95,<br />
|
||
|
'name' => 'Coffee Mug'<br />
|
||
|
),<br />
|
||
|
|
||
|
array(<br />
|
||
|
'id' => 'sku_965QRS',<br />
|
||
|
'qty' => 1,<br />
|
||
|
'price' => 29.95,<br />
|
||
|
'name' => 'Shot Glass'<br />
|
||
|
)<br />
|
||
|
|
||
|
);<br />
|
||
|
<br />
|
||
|
|
||
|
$this->cart->insert($data);
|
||
|
|
||
|
</code>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<h2>Displaying the Cart</h2>
|
||
|
|
||
|
<p>To display the cart you will create a <a href="../general/views.html">view file</a> with code similar to the one shown below.</p>
|
||
|
|
||
|
<p>Please note that this example uses the <a href="../helpers/form_helper.html">form helper</a>.</p>
|
||
|
|
||
|
|
||
|
<textarea class="textarea" style="width:100%" cols="50" rows="55">
|
||
|
<?php echo form_open('path/to/controller/update/function'); ?>
|
||
|
|
||
|
<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>
|
||
|
</textarea>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<h2>Updating The Cart</h2>
|
||
|
|
||
|
<p>To update the information in your cart, you must pass an array containing the <kbd>Row ID</kbd> and quantity to the <dfn>$this->cart->update()</dfn> function:</p>
|
||
|
|
||
|
<p class="important"><strong>Note:</strong> If the quantity is set to zero, the item will be removed from the cart.</p>
|
||
|
|
||
|
<code>
|
||
|
$data = array(<br />
|
||
|
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',<br />
|
||
|
'qty' => 3<br />
|
||
|
);<br />
|
||
|
<br />
|
||
|
|
||
|
$this->cart->update($data);
|
||
|
<br /><br />
|
||
|
// Or a multi-dimensional array<br /><br />
|
||
|
$data = array(<br />
|
||
|
|
||
|
array(<br />
|
||
|
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',<br />
|
||
|
'qty' => 3<br />
|
||
|
),<br />
|
||
|
|
||
|
array(<br />
|
||
|
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',<br />
|
||
|
'qty' => 4<br />
|
||
|
),<br />
|
||
|
|
||
|
array(<br />
|
||
|
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',<br />
|
||
|
'qty' => 2<br />
|
||
|
)<br />
|
||
|
|
||
|
);<br />
|
||
|
<br />
|
||
|
|
||
|
$this->cart->update($data);
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
</code>
|
||
|
|
||
|
<p><strong>What is a Row ID?</strong> The <kbd>row ID</kbd> is a unique identifier that is generated by the cart code when an item is added to the cart. The reason a
|
||
|
unique ID is created is so that identical products with different options can be managed by the cart.</p>
|
||
|
|
||
|
<p>For example, let's say someone buys two identical t-shirts (same product ID), but in different sizes. The product ID (and other attributes) will be
|
||
|
identical for both sizes because it's the same shirt. The only difference will be the size. The cart must therefore have a means of identifying this
|
||
|
difference so that the two sizes of shirts can be managed independently. It does so by creating a unique "row ID" based on the product ID and any options associated with it.</p>
|
||
|
|
||
|
<p>In nearly all cases, updating the cart will be something the user does via the "view cart" page, so as a developer, it is unlikely that you will ever have to concern yourself
|
||
|
with the "row ID", other then making sure your "view cart" page contains this information in a hidden form field, and making sure it gets passed to the update
|
||
|
function when the update form is submitted. Please examine the construction of the "view cart" page above for more information.</p>
|
||
|
|
||
|
|
||
|
|
||
|
<p> <br /></p>
|
||
|
|
||
|
|
||
|
<h1>Function Reference</h1>
|
||
|
|
||
|
<h2>$this->cart->insert();</h2>
|
||
|
|
||
|
<p>Permits you to add items to the shopping cart, as outlined above.</p>
|
||
|
|
||
|
|
||
|
<h2>$this->cart->update();</h2>
|
||
|
|
||
|
<p>Permits you to update items in the shopping cart, as outlined above.</p>
|
||
|
|
||
|
|
||
|
<h2>$this->cart->total();</h2>
|
||
|
|
||
|
<p>Displays the total amount in the cart.</p>
|
||
|
|
||
|
|
||
|
<h2>$this->cart->total_items();</h2>
|
||
|
|
||
|
<p>Displays the total number of items in the cart.</p>
|
||
|
|
||
|
|
||
|
<h2>$this->cart->contents();</h2>
|
||
|
|
||
|
<p>Returns an array containing everything in the cart.</p>
|
||
|
|
||
|
|
||
|
|
||
|
<h2>$this->cart->has_options(rowid);</h2>
|
||
|
|
||
|
<p>Returns TRUE (boolean) if a particular row in the cart contains options. This function is designed to be used in a loop with <dfn>$this->cart->contents()</dfn>, since you must pass the <kbd>rowid</kbd> to this function, as shown in the <dfn>Displaying the Cart</dfn> example above.</p>
|
||
|
|
||
|
|
||
|
<h2>$this->cart->options(rowid);</h2>
|
||
|
|
||
|
<p>Returns an array of options for a particular product. This function is designed to be used in a loop with <dfn>$this->cart->contents()</dfn>, since you must pass the <kbd>rowid</kbd> to this function, as shown in the <dfn>Displaying the Cart</dfn> example above.</p>
|
||
|
|
||
|
|
||
|
|
||
|
<h2>$this->cart->destroy();</h2>
|
||
|
|
||
|
<p>Permits you to destroy the cart. This function will likely be called when you are finished processing the customer's order.</p>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
</div>
|
||
|
<!-- END CONTENT -->
|
||
|
|
||
|
|
||
|
<div id="footer">
|
||
|
<p>
|
||
|
Previous Topic: <a href="calendar.html">Calendar Class</a>
|
||
|
·
|
||
|
<a href="#top">Top of Page</a> ·
|
||
|
<a href="../index.html">User Guide Home</a> ·
|
||
|
Next Topic: <a href="config.html">Config Class</a>
|
||
|
</p>
|
||
|
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2010 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|