Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################### |
| 2 | Shopping Cart Class |
| 3 | ################### |
| 4 | |
| 5 | The Cart Class permits items to be added to a session that stays active |
| 6 | while a user is browsing your site. These items can be retrieved and |
| 7 | displayed in a standard "shopping cart" format, allowing the user to |
| 8 | update the quantity or remove items from the cart. |
| 9 | |
| 10 | Please note that the Cart Class ONLY provides the core "cart" |
| 11 | functionality. It does not provide shipping, credit card authorization, |
| 12 | or other processing components. |
| 13 | |
| 14 | .. contents:: Page Contents |
| 15 | |
| 16 | Initializing the Shopping Cart Class |
| 17 | ==================================== |
| 18 | |
| 19 | .. important:: The Cart class utilizes CodeIgniter's :doc:`Session |
| 20 | Class <sessions>` to save the cart information to a database, so |
| 21 | before using the Cart class you must set up a database table as |
| 22 | indicated in the :doc:`Session Documentation <sessions>`, and set the |
| 23 | session preferences in your application/config/config.php file to |
| 24 | utilize a database. |
| 25 | |
| 26 | To initialize the Shopping Cart Class in your controller constructor, |
| 27 | use the $this->load->library function:: |
| 28 | |
| 29 | $this->load->library('cart'); |
| 30 | |
| 31 | Once loaded, the Cart object will be available using:: |
| 32 | |
| 33 | $this->cart |
| 34 | |
| 35 | .. note:: The Cart Class will load and initialize the Session Class |
| 36 | automatically, so unless you are using sessions elsewhere in your |
| 37 | application, you do not need to load the Session class. |
| 38 | |
| 39 | Adding an Item to The Cart |
| 40 | ========================== |
| 41 | |
| 42 | To add an item to the shopping cart, simply pass an array with the |
| 43 | product information to the $this->cart->insert() function, as shown |
| 44 | below:: |
| 45 | |
| 46 | $data = array( |
Derek Jones | 97f283d | 2011-10-05 16:17:25 -0500 | [diff] [blame] | 47 | 'id' => 'sku_123ABC', |
| 48 | 'qty' => 1, |
| 49 | 'price' => 39.95, |
| 50 | 'name' => 'T-Shirt', |
| 51 | 'options' => array('Size' => 'L', 'Color' => 'Red') |
| 52 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
| 54 | $this->cart->insert($data); |
| 55 | |
| 56 | .. important:: The first four array indexes above (id, qty, price, and |
| 57 | name) are **required**. If you omit any of them the data will not be |
| 58 | saved to the cart. The fifth index (options) is optional. It is intended |
| 59 | to be used in cases where your product has options associated with it. |
| 60 | Use an array for options, as shown above. |
| 61 | |
| 62 | The five reserved indexes are: |
| 63 | |
| 64 | - **id** - Each product in your store must have a unique identifier. |
| 65 | Typically this will be an "sku" or other such identifier. |
| 66 | - **qty** - The quantity being purchased. |
| 67 | - **price** - The price of the item. |
| 68 | - **name** - The name of the item. |
| 69 | - **options** - Any additional attributes that are needed to identify |
| 70 | the product. These must be passed via an array. |
| 71 | |
| 72 | In addition to the five indexes above, there are two reserved words: |
| 73 | rowid and subtotal. These are used internally by the Cart class, so |
| 74 | please do NOT use those words as index names when inserting data into |
| 75 | the cart. |
| 76 | |
| 77 | Your array may contain additional data. Anything you include in your |
| 78 | array will be stored in the session. However, it is best to standardize |
| 79 | your data among all your products in order to make displaying the |
| 80 | information in a table easier. |
| 81 | |
| 82 | The insert() method will return the $rowid if you successfully insert a |
| 83 | single item. |
| 84 | |
| 85 | Adding Multiple Items to The Cart |
| 86 | ================================= |
| 87 | |
| 88 | By using a multi-dimensional array, as shown below, it is possible to |
| 89 | add multiple products to the cart in one action. This is useful in cases |
| 90 | where you wish to allow people to select from among several items on the |
| 91 | same page. |
| 92 | |
| 93 | :: |
| 94 | |
| 95 | $data = array( |
Derek Jones | 97f283d | 2011-10-05 16:17:25 -0500 | [diff] [blame] | 96 | array( |
| 97 | 'id' => 'sku_123ABC', |
| 98 | 'qty' => 1, |
| 99 | 'price' => 39.95, |
| 100 | 'name' => 'T-Shirt', |
| 101 | 'options' => array('Size' => 'L', 'Color' => 'Red') |
| 102 | ), |
| 103 | array( |
| 104 | 'id' => 'sku_567ZYX', |
| 105 | 'qty' => 1, |
| 106 | 'price' => 9.95, |
| 107 | 'name' => 'Coffee Mug' |
| 108 | ), |
| 109 | array( |
| 110 | 'id' => 'sku_965QRS', |
| 111 | 'qty' => 1, |
| 112 | 'price' => 29.95, |
| 113 | 'name' => 'Shot Glass' |
| 114 | ) |
| 115 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
| 117 | $this->cart->insert($data); |
| 118 | |
| 119 | Displaying the Cart |
| 120 | =================== |
| 121 | |
| 122 | To display the cart you will create a :doc:`view |
| 123 | file </general/views>` with code similar to the one shown below. |
| 124 | |
| 125 | Please note that this example uses the :doc:`form |
| 126 | helper </helpers/form_helper>`. |
| 127 | |
| 128 | :: |
| 129 | |
| 130 | <?php echo form_open('path/to/controller/update/function'); ?> |
| 131 | |
| 132 | <table cellpadding="6" cellspacing="1" style="width:100%" border="0"> |
| 133 | |
| 134 | <tr> |
| 135 | <th>QTY</th> |
| 136 | <th>Item Description</th> |
| 137 | <th style="text-align:right">Item Price</th> |
| 138 | <th style="text-align:right">Sub-Total</th> |
| 139 | </tr> |
| 140 | |
| 141 | <?php $i = 1; ?> |
| 142 | |
| 143 | <?php foreach ($this->cart->contents() as $items): ?> |
| 144 | |
| 145 | <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?> |
| 146 | |
| 147 | <tr> |
| 148 | <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td> |
| 149 | <td> |
| 150 | <?php echo $items['name']; ?> |
| 151 | |
| 152 | <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?> |
| 153 | |
| 154 | <p> |
| 155 | <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?> |
| 156 | |
| 157 | <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br /> |
| 158 | |
| 159 | <?php endforeach; ?> |
| 160 | </p> |
| 161 | |
| 162 | <?php endif; ?> |
| 163 | |
| 164 | </td> |
| 165 | <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td> |
| 166 | <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td> |
| 167 | </tr> |
| 168 | |
| 169 | <?php $i++; ?> |
| 170 | |
| 171 | <?php endforeach; ?> |
| 172 | |
| 173 | <tr> |
| 174 | <td colspan="2"> </td> |
| 175 | <td class="right"><strong>Total</strong></td> |
| 176 | <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td> |
| 177 | </tr> |
| 178 | |
| 179 | </table> |
| 180 | |
| 181 | <p><?php echo form_submit('', 'Update your Cart'); ?></p> |
| 182 | |
| 183 | Updating The Cart |
| 184 | ================= |
| 185 | |
| 186 | To update the information in your cart, you must pass an array |
| 187 | containing the Row ID and quantity to the $this->cart->update() |
| 188 | function: |
| 189 | |
| 190 | .. note:: If the quantity is set to zero, the item will be removed from |
| 191 | the cart. |
| 192 | |
| 193 | :: |
| 194 | |
| 195 | $data = array( |
Derek Jones | 97f283d | 2011-10-05 16:17:25 -0500 | [diff] [blame] | 196 | 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', |
| 197 | 'qty' => 3 |
| 198 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 199 | |
| 200 | $this->cart->update($data); |
| 201 | |
| 202 | // Or a multi-dimensional array |
| 203 | |
| 204 | $data = array( |
Derek Jones | 97f283d | 2011-10-05 16:17:25 -0500 | [diff] [blame] | 205 | array( |
| 206 | 'rowid' => 'b99ccdf16028f015540f341130b6d8ec', |
| 207 | 'qty' => 3 |
| 208 | ), |
| 209 | array( |
| 210 | 'rowid' => 'xw82g9q3r495893iajdh473990rikw23', |
| 211 | 'qty' => 4 |
| 212 | ), |
| 213 | array( |
| 214 | 'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333', |
| 215 | 'qty' => 2 |
| 216 | ) |
| 217 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 218 | |
Derek Jones | 97f283d | 2011-10-05 16:17:25 -0500 | [diff] [blame] | 219 | $this->cart->update($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | |
| 221 | What is a Row ID? |
| 222 | ***************** |
| 223 | |
| 224 | The row ID is a unique identifier that is |
| 225 | generated by the cart code when an item is added to the cart. The reason |
| 226 | a unique ID is created is so that identical products with different |
| 227 | options can be managed by the cart. |
| 228 | |
| 229 | For example, let's say someone buys two identical t-shirts (same product |
| 230 | ID), but in different sizes. The product ID (and other attributes) will |
| 231 | be identical for both sizes because it's the same shirt. The only |
| 232 | difference will be the size. The cart must therefore have a means of |
| 233 | identifying this difference so that the two sizes of shirts can be |
| 234 | managed independently. It does so by creating a unique "row ID" based on |
| 235 | the product ID and any options associated with it. |
| 236 | |
| 237 | In nearly all cases, updating the cart will be something the user does |
| 238 | via the "view cart" page, so as a developer, it is unlikely that you |
| 239 | will ever have to concern yourself with the "row ID", other then making |
| 240 | sure your "view cart" page contains this information in a hidden form |
| 241 | field, and making sure it gets passed to the update function when the |
| 242 | update form is submitted. Please examine the construction of the "view |
| 243 | cart" page above for more information. |
| 244 | |
| 245 | |
| 246 | Function Reference |
| 247 | ================== |
| 248 | |
| 249 | $this->cart->insert(); |
| 250 | ********************** |
| 251 | |
| 252 | Permits you to add items to the shopping cart, as outlined above. |
| 253 | |
| 254 | $this->cart->update(); |
| 255 | ********************** |
| 256 | |
| 257 | Permits you to update items in the shopping cart, as outlined above. |
| 258 | |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 259 | $this->cart->remove(rowid); |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame^] | 260 | *************************** |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 261 | |
| 262 | Allows you to remove an item from the shopping cart by passing it the rowid. |
| 263 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 264 | $this->cart->total(); |
| 265 | ********************* |
| 266 | |
| 267 | Displays the total amount in the cart. |
| 268 | |
| 269 | $this->cart->total_items(); |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame^] | 270 | *************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 271 | |
| 272 | Displays the total number of items in the cart. |
| 273 | |
Andrew Seymour | de2e96a | 2011-12-13 16:44:59 +0000 | [diff] [blame] | 274 | $this->cart->contents(boolean); |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame^] | 275 | ******************************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 276 | |
Andrew Seymour | de2e96a | 2011-12-13 16:44:59 +0000 | [diff] [blame] | 277 | Returns an array containing everything in the cart. You can sort the order, |
| 278 | by which this is returned by passing it "true" where the contents will be sorted |
| 279 | from newest to oldest, by leaving this function blank, you'll automatically just get |
| 280 | first added to the basket to last added to the basket. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 281 | |
| 282 | $this->cart->has_options(rowid); |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame^] | 283 | ******************************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 284 | |
| 285 | Returns TRUE (boolean) if a particular row in the cart contains options. |
| 286 | This function is designed to be used in a loop with |
| 287 | $this->cart->contents(), since you must pass the rowid to this function, |
| 288 | as shown in the Displaying the Cart example above. |
| 289 | |
| 290 | $this->cart->product_options(rowid); |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame^] | 291 | ************************************ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 292 | |
| 293 | Returns an array of options for a particular product. This function is |
| 294 | designed to be used in a loop with $this->cart->contents(), since you |
| 295 | must pass the rowid to this function, as shown in the Displaying the |
| 296 | Cart example above. |
| 297 | |
| 298 | $this->cart->destroy(); |
| 299 | *********************** |
| 300 | |
| 301 | Permits you to destroy the cart. This function will likely be called |
| 302 | when you are finished processing the customer's order. |