blob: 81b43e3637994f78c9d6155b8743a8ba9849b265 [file] [log] [blame]
Derek Allardefb37ca2009-09-16 09:47:52 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>Shopping Cart Class : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Pascal Kriete1f622292011-04-07 12:06:51 -040031<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
Derek Allardefb37ca2009-09-16 09:47:52 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Shopping Cart Class
46</td>
47<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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
48</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>Shopping Cart Class</h1>
60
Barry Mienydd671972010-10-04 16:33:58 +020061<p>The Cart Class permits items to be added to a session that stays active while a user is browsing your site.
Derek Allardefb37ca2009-09-16 09:47:52 +000062These 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>
63
Phil Sturgeon90910512011-07-20 10:07:40 -060064<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>
Derek Allardefb37ca2009-09-16 09:47:52 +000065
66
67<h2>Initializing the Shopping Cart Class</h2>
68
69<p><strong>Important:</strong> The Cart class utilizes CodeIgniter's
Derek Jones37f4b9c2011-07-01 17:56:50 -050070<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
Derek Allardd8153f52010-08-16 08:57:45 -040071as indicated in the <a href="sessions.html">Session Documentation</a> , and set the session preferences in your <kbd>application/config/config.php</kbd> file to utilize a database.</p>
Derek Allardefb37ca2009-09-16 09:47:52 +000072
73<p>To initialize the Shopping Cart Class in your controller constructor, use the <dfn>$this->load->library</dfn> function:</p>
74
75<code>$this->load->library('cart');</code>
76<p>Once loaded, the Cart object will be available using: <dfn>$this->cart</dfn></p>
77
78<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>
79
80<h2>Adding an Item to The Cart</h2>
81
82<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>
83
84<code>
85$data = array(<br />
86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; => 'sku_123ABC',<br />
87&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'qty'&nbsp;&nbsp;&nbsp;&nbsp; => 1,<br />
88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'price'&nbsp;&nbsp; => 39.95,<br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;&nbsp;&nbsp; => 'T-Shirt',<br />
90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'options' => array('Size' => 'L', 'Color' => 'Red')<br />
91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
92<br />
93
94$this->cart->insert($data);
95
96</code>
97
98<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>.
99If you omit any of them the data will not be saved to the cart. The fifth index (<dfn>options</dfn>) is optional.
100It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.</p>
101
102<p>The five reserved indexes are:</p>
103
104<ul>
105<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>
106<li><strong>qty</strong> - The quantity being purchased.
107<li><strong>price</strong> - The price of the item.
108<li><strong>name</strong> - The name of the item.
Phil Sturgeon90910512011-07-20 10:07:40 -0600109<li><strong>options</strong> - Any additional attributes that are needed to identify the product. These must be passed via an array.
Derek Allardefb37ca2009-09-16 09:47:52 +0000110</ul>
111
Phil Sturgeon90910512011-07-20 10:07:40 -0600112<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>
Derek Allardefb37ca2009-09-16 09:47:52 +0000113
Phil Sturgeon90910512011-07-20 10:07:40 -0600114<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>
115
116<p>The insert() method will return the $rowid if you successfully insert a single item.</p>
Derek Allardefb37ca2009-09-16 09:47:52 +0000117
118
119<h2>Adding Multiple Items to The Cart</h2>
120
Phil Sturgeon90910512011-07-20 10:07:40 -0600121<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>
Derek Allardefb37ca2009-09-16 09:47:52 +0000122
123
124<code>
125$data = array(<br />
126
127&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; => 'sku_123ABC',<br />
129&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'qty'&nbsp;&nbsp;&nbsp;&nbsp; => 1,<br />
145&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'price'&nbsp;&nbsp; => 29.95,<br />
146&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;&nbsp;&nbsp; => 'Shot Glass'<br />
147&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
148
149&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
150<br />
151
152$this->cart->insert($data);
153
154</code>
155
156
157
158
159<h2>Displaying the Cart</h2>
160
161<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>
162
163<p>Please note that this example uses the <a href="../helpers/form_helper.html">form helper</a>.</p>
164
165
166<textarea class="textarea" style="width:100%" cols="50" rows="55">
167&lt;?php echo form_open('path/to/controller/update/function'); ?>
168
169&lt;table cellpadding="6" cellspacing="1" style="width:100%" border="0">
170
171&lt;tr>
Derek Jones37f4b9c2011-07-01 17:56:50 -0500172 &lt;th>QTY&lt;/th>
173 &lt;th>Item Description&lt;/th>
174 &lt;th style="text-align:right">Item Price&lt;/th>
175 &lt;th style="text-align:right">Sub-Total&lt;/th>
Derek Allardefb37ca2009-09-16 09:47:52 +0000176&lt;/tr>
177
178&lt;?php $i = 1; ?>
179
Pascal Krietefeba2de2011-02-14 13:25:30 -0500180&lt;?php foreach ($this->cart->contents() as $items): ?>
Derek Allardefb37ca2009-09-16 09:47:52 +0000181
182 &lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allardefb37ca2009-09-16 09:47:52 +0000184 &lt;tr>
Derek Jones37f4b9c2011-07-01 17:56:50 -0500185 &lt;td>&lt;?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>&lt;/td>
186 &lt;td>
Derek Allardefb37ca2009-09-16 09:47:52 +0000187 &lt;?php echo $items['name']; ?>
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allardefb37ca2009-09-16 09:47:52 +0000189 &lt;?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allardefb37ca2009-09-16 09:47:52 +0000191 &lt;p>
192 &lt;?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allardefb37ca2009-09-16 09:47:52 +0000194 &lt;strong>&lt;?php echo $option_name; ?>:&lt;/strong> &lt;?php echo $option_value; ?>&lt;br />
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allardefb37ca2009-09-16 09:47:52 +0000196 &lt;?php endforeach; ?>
197 &lt;/p>
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allardefb37ca2009-09-16 09:47:52 +0000199 &lt;?php endif; ?>
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Jones37f4b9c2011-07-01 17:56:50 -0500201 &lt;/td>
202 &lt;td style="text-align:right">&lt;?php echo $this->cart->format_number($items['price']); ?>&lt;/td>
203 &lt;td style="text-align:right">$&lt;?php echo $this->cart->format_number($items['subtotal']); ?>&lt;/td>
Derek Allardefb37ca2009-09-16 09:47:52 +0000204 &lt;/tr>
205
206&lt;?php $i++; ?>
207
208&lt;?php endforeach; ?>
209
210&lt;tr>
Derek Jones37f4b9c2011-07-01 17:56:50 -0500211 &lt;td colspan="2">&nbsp;&lt;/td>
212 &lt;td class="right">&lt;strong>Total&lt;/strong>&lt;/td>
213 &lt;td class="right">$&lt;?php echo $this->cart->format_number($this->cart->total()); ?>&lt;/td>
Derek Allardefb37ca2009-09-16 09:47:52 +0000214&lt;/tr>
215
216&lt;/table>
217
218&lt;p>&lt;?php echo form_submit('', 'Update your Cart'); ?>&lt;/p>
219</textarea>
220
221
222
223
224<h2>Updating The Cart</h2>
225
226<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>
227
228<p class="important"><strong>Note:</strong> If the quantity is set to zero, the item will be removed from the cart.</p>
229
230<code>
231$data = array(<br />
232&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rowid' => 'b99ccdf16028f015540f341130b6d8ec',<br />
233&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'qty'&nbsp;&nbsp; => 3<br />
234&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
235<br />
236
237$this->cart->update($data);
238<br /><br />
239// Or a multi-dimensional array<br /><br />
240$data = array(<br />
241
242&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
243&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
253&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rowid'&nbsp;&nbsp; => 'fh4kdkkkaoe30njgoe92rkdkkobec333',<br />
254&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'qty'&nbsp;&nbsp;&nbsp;&nbsp; => 2<br />
255&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
256
257&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
258<br />
259
260$this->cart->update($data);
261
262
263
264
265</code>
266
Phil Sturgeon90910512011-07-20 10:07:40 -0600267<p><strong>What is a Row ID?</strong>&nbsp; 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
Derek Allardefb37ca2009-09-16 09:47:52 +0000268unique ID is created is so that identical products with different options can be managed by the cart.</p>
269
Phil Sturgeon90910512011-07-20 10:07:40 -0600270<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
271identical 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
Derek Allardefb37ca2009-09-16 09:47:52 +0000272difference 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>
273
274<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
275with 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
276function when the update form is submitted. Please examine the construction of the "view cart" page above for more information.</p>
277
278
279
280<p>&nbsp;<br /></p>
281
282
283<h1>Function Reference</h1>
284
285<h2>$this->cart->insert();</h2>
286
287<p>Permits you to add items to the shopping cart, as outlined above.</p>
288
289
290<h2>$this->cart->update();</h2>
291
292<p>Permits you to update items in the shopping cart, as outlined above.</p>
293
294
295<h2>$this->cart->total();</h2>
296
297<p>Displays the total amount in the cart.</p>
298
299
300<h2>$this->cart->total_items();</h2>
301
302<p>Displays the total number of items in the cart.</p>
303
304
305<h2>$this->cart->contents();</h2>
306
307<p>Returns an array containing everything in the cart.</p>
308
309
310
311<h2>$this->cart->has_options(rowid);</h2>
312
Phil Sturgeon90910512011-07-20 10:07:40 -0600313<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>
Derek Allardefb37ca2009-09-16 09:47:52 +0000314
315
Greg Akerb32b82a2010-11-09 17:01:18 -0600316<h2>$this->cart->product_options(rowid);</h2>
Derek Allardefb37ca2009-09-16 09:47:52 +0000317
318<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>
319
320
321
322<h2>$this->cart->destroy();</h2>
323
Phil Sturgeon90910512011-07-20 10:07:40 -0600324<p>Permits you to destroy the cart. This function will likely be called when you are finished processing the customer's order.</p>
Derek Allardefb37ca2009-09-16 09:47:52 +0000325
326
327
328
329</div>
330<!-- END CONTENT -->
331
332
333<div id="footer">
334<p>
335Previous Topic:&nbsp;&nbsp;<a href="calendar.html">Calendar Class</a>
336&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
337<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
338<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
339Next Topic:&nbsp;&nbsp;<a href="config.html">Config Class</a>
340</p>
Derek Jones700205a2011-01-28 07:44:28 -0600341<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allardefb37ca2009-09-16 09:47:52 +0000342</div>
343
344</body>
Rick Ellisac69d422009-02-17 02:28:10 +0000345</html>