Rick Ellis | ac69d42 | 2009-02-17 02:28:10 +0000 | [diff] [blame] | 1 | <!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>
|
| 31 | <td><h1>CodeIgniter User Guide Version 1.7</h1></td>
|
| 32 | <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> ›
|
| 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Shopping 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 <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>
|
| 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 |
|
| 61 | <p>The Cart Class permits items to be added to a session that stays active while a user is browsing your site.
|
| 62 | 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>
|
| 63 |
|
| 64 | <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>
|
| 65 |
|
| 66 |
|
| 67 | <h2>Initializing the Shopping Cart Class</h2>
|
| 68 |
|
| 69 | <p><strong>Important:</strong> The Cart class utilizes CodeIgniter's
|
| 70 | <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
|
| 71 | 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>
|
| 72 |
|
| 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 | 'id' => 'sku_123ABC',<br />
|
| 87 | 'qty' => 1,<br />
|
| 88 | 'price' => 39.95,<br />
|
| 89 | 'name' => 'T-Shirt',<br />
|
| 90 | 'options' => array('Size' => 'L', 'Color' => 'Red')<br />
|
| 91 | );<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>.
|
| 99 | If you omit any of them the data will not be saved to the cart. The fifth index (<dfn>options</dfn>) is optional.
|
| 100 | 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>
|
| 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.
|
| 109 | <li><strong>options</strong> - Any additional attributes that are needed to identify the product. These must be passed via an array.
|
| 110 | </ul>
|
| 111 |
|
| 112 | <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
|
| 113 | please do NOT use those words as index names when inserting data into the cart.</p>
|
| 114 |
|
| 115 | <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
|
| 116 | all your products in order to make displaying the information in a table easier.</p>
|
| 117 |
|
| 118 |
|
| 119 | <h2>Adding Multiple Items to The Cart</h2>
|
| 120 |
|
| 121 | <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
|
| 122 | people to select from among several items on the same page.</p>
|
| 123 |
|
| 124 |
|
| 125 | <code>
|
| 126 | $data = array(<br />
|
| 127 |
|
| 128 | array(<br />
|
| 129 | 'id' => 'sku_123ABC',<br />
|
| 130 | 'qty' => 1,<br />
|
| 131 | 'price' => 39.95,<br />
|
| 132 | 'name' => 'T-Shirt',<br />
|
| 133 | 'options' => array('Size' => 'L', 'Color' => 'Red')<br />
|
| 134 | ),<br />
|
| 135 |
|
| 136 | array(<br />
|
| 137 | 'id' => 'sku_567ZYX',<br />
|
| 138 | 'qty' => 1,<br />
|
| 139 | 'price' => 9.95,<br />
|
| 140 | 'name' => 'Coffee Mug'<br />
|
| 141 | ),<br />
|
| 142 |
|
| 143 | array(<br />
|
| 144 | 'id' => 'sku_965QRS',<br />
|
| 145 | 'qty' => 1,<br />
|
| 146 | 'price' => 29.95,<br />
|
| 147 | 'name' => 'Shot Glass'<br />
|
| 148 | )<br />
|
| 149 |
|
| 150 | );<br />
|
| 151 | <br />
|
| 152 |
|
| 153 | $this->cart->insert($data);
|
| 154 |
|
| 155 | </code>
|
| 156 |
|
| 157 |
|
| 158 | <h2>Updating The Cart</h2>
|
| 159 |
|
| 160 | <p>To update the information in your cart, pass an array containing the product ID and quantity to the <dfn>$this->cart->update()</dfn> function:</p>
|
| 161 |
|
| 162 |
|
| 163 | <code>
|
| 164 | $data = array(<br />
|
| 165 | 'id' => '123XYZ',<br />
|
| 166 | 'qty' => 3<br />
|
| 167 | );<br />
|
| 168 | <br />
|
| 169 |
|
| 170 | $this->cart->update($data);
|
| 171 |
|
| 172 | </code>
|
| 173 |
|
| 174 | <p>Just as you can when inserting, you may use a multi-dimensional array in order to update multiple items simultaneously:</p>
|
| 175 |
|
| 176 |
|
| 177 | <code>
|
| 178 | $data = array(<br />
|
| 179 |
|
| 180 | array(<br />
|
| 181 | 'id' => 'sku_123ABC',<br />
|
| 182 | 'qty' => 3<br />
|
| 183 | ),<br />
|
| 184 |
|
| 185 | array(<br />
|
| 186 | 'id' => 'sku_567ZYX',<br />
|
| 187 | 'qty' => 4<br />
|
| 188 | ),<br />
|
| 189 |
|
| 190 | array(<br />
|
| 191 | 'id' => 'sku_965QRS',<br />
|
| 192 | 'qty' => 2<br />
|
| 193 | )<br />
|
| 194 |
|
| 195 | );<br />
|
| 196 | <br />
|
| 197 |
|
| 198 | $this->cart->update($data);
|
| 199 |
|
| 200 | </code>
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 | <h2>Deleting an Item From The Cart</h2>
|
| 206 |
|
| 207 | <p>To delete an item from your cart, pass an array containing the product ID and a quantity of zero to the <dfn>$this->cart->update()</dfn> function:</p>
|
| 208 |
|
| 209 |
|
| 210 | <code>
|
| 211 | $data = array(<br />
|
| 212 | 'id' => '123XYZ',<br />
|
| 213 | 'qty' => 0<br />
|
| 214 | );<br />
|
| 215 | <br />
|
| 216 |
|
| 217 | $this->cart->update($data);
|
| 218 |
|
| 219 | </code>
|
| 220 |
|
| 221 |
|
| 222 | <p>You may use a multi-dimensional array in order to delete multiple items simultaneously:</p>
|
| 223 |
|
| 224 |
|
| 225 | <code>
|
| 226 | $data = array(<br />
|
| 227 |
|
| 228 | array(<br />
|
| 229 | 'id' => 'sku_123ABC',<br />
|
| 230 | 'qty' => 0<br />
|
| 231 | ),<br />
|
| 232 |
|
| 233 | array(<br />
|
| 234 | 'id' => 'sku_567ZYX',<br />
|
| 235 | 'qty' => 0<br />
|
| 236 | ),<br />
|
| 237 |
|
| 238 | array(<br />
|
| 239 | 'id' => 'sku_965QRS',<br />
|
| 240 | 'qty' => 0<br />
|
| 241 | )<br />
|
| 242 |
|
| 243 | );<br />
|
| 244 | <br />
|
| 245 |
|
| 246 | $this->cart->update($data);
|
| 247 |
|
| 248 | </code>
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 | <h2>Displaying the Cart</h2>
|
| 253 |
|
| 254 | <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>
|
| 255 |
|
| 256 | <p>Please note that this example uses the <a href="../helpers/form_helper.html">form helper</a>.</p>
|
| 257 |
|
| 258 |
|
| 259 | <textarea class="textarea" style="width:100%" cols="50" rows="55">
|
| 260 | <?php echo form_open('path/to/controller/update/function'); ?>
|
| 261 |
|
| 262 | <table cellpadding="6" cellspacing="1" style="width:100%" border="0">
|
| 263 |
|
| 264 | <tr>
|
| 265 | <th>QTY</th>
|
| 266 | <th>Item Description</th>
|
| 267 | <th style="text-align:right">Item Price</th>
|
| 268 | <th style="text-align:right">Sub-Total</th>
|
| 269 | </tr>
|
| 270 |
|
| 271 | <?php $i = 1; ?>
|
| 272 |
|
| 273 | <?php foreach($this->cart->contents() as $items): ?>
|
| 274 |
|
| 275 | <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
|
| 276 |
|
| 277 | <tr>
|
| 278 | <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
|
| 279 | <td>
|
| 280 | <?php echo $items['name']; ?>
|
| 281 |
|
| 282 | <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
|
| 283 |
|
| 284 | <p>
|
| 285 | <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
|
| 286 |
|
| 287 | <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
|
| 288 |
|
| 289 | <?php endforeach; ?>
|
| 290 | </p>
|
| 291 |
|
| 292 | <?php endif; ?>
|
| 293 |
|
| 294 | </td>
|
| 295 | <td style="text-align:right"><?php echo $items['price']; ?></td>
|
| 296 | <td style="text-align:right">$<?php echo $items['subtotal']; ?></td>
|
| 297 | </tr>
|
| 298 |
|
| 299 | <?php $i++; ?>
|
| 300 |
|
| 301 | <?php endforeach; ?>
|
| 302 |
|
| 303 | <tr>
|
| 304 | <td colspan="2"> </td>
|
| 305 | <td class="right"><strong>Total</strong></td>
|
| 306 | <td class="right">$<?php echo $this->cart->total(); ?></td>
|
| 307 | </tr>
|
| 308 |
|
| 309 | </table>
|
| 310 |
|
| 311 | <p><?php echo form_submit('', 'Update your Cart'); ?></p>
|
| 312 | </textarea>
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 | <p> <br /></p>
|
| 317 |
|
| 318 |
|
| 319 | <h1>Function Reference</h1>
|
| 320 |
|
| 321 | <h2>$this->cart->insert();</h2>
|
| 322 |
|
| 323 | <p>Permits you to add items to the shopping cart, as outlined above.</p>
|
| 324 |
|
| 325 |
|
| 326 | <h2>$this->cart->update();</h2>
|
| 327 |
|
| 328 | <p>Permits you to update items in the shopping cart, as outlined above.</p>
|
| 329 |
|
| 330 |
|
| 331 | <h2>$this->cart->total();</h2>
|
| 332 |
|
| 333 | <p>Displays the total amount in the cart.</p>
|
| 334 |
|
| 335 |
|
| 336 | <h2>$this->cart->total_items();</h2>
|
| 337 |
|
| 338 | <p>Displays the total number of items in the cart.</p>
|
| 339 |
|
| 340 |
|
| 341 | <h2>$this->cart->contents();</h2>
|
| 342 |
|
| 343 | <p>Returns an array containing everything in the cart.</p>
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 | <h2>$this->cart->has_options(rowid);</h2>
|
| 348 |
|
| 349 | <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>
|
| 350 |
|
| 351 |
|
| 352 | <h2>$this->cart->options(rowid);</h2>
|
| 353 |
|
| 354 | <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>
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 | <h2>$this->cart->destroy();</h2>
|
| 359 |
|
| 360 | <p>Permits you to destroy the cart. This function will likely be called when you are finished processing the customer's order.</p>
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 | </div>
|
| 366 | <!-- END CONTENT -->
|
| 367 |
|
| 368 |
|
| 369 | <div id="footer">
|
| 370 | <p>
|
| 371 | Previous Topic: <a href="benchmark.html">Benchmarking Class</a>
|
| 372 | ·
|
| 373 | <a href="#top">Top of Page</a> ·
|
| 374 | <a href="../index.html">User Guide Home</a> ·
|
| 375 | Next Topic: <a href="calendar.html">Calendar Class</a>
|
| 376 | </p>
|
| 377 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
| 378 | </div>
|
| 379 |
|
| 380 | </body>
|
| 381 | </html> |