blob: 74e7915540c171537d3cfbf0c6dbcd540ef2a131 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###################
2Shopping Cart Class
3###################
4
5The Cart Class permits items to be added to a session that stays active
6while a user is browsing your site. These items can be retrieved and
7displayed in a standard "shopping cart" format, allowing the user to
8update the quantity or remove items from the cart.
9
10Please note that the Cart Class ONLY provides the core "cart"
11functionality. It does not provide shipping, credit card authorization,
12or other processing components.
13
Derek Jones82b34e62013-07-21 23:07:20 -070014.. contents::
15 :local:
16
17.. raw:: html
18
19 <div class="custom-index container"></div>
20
21********************
22Using the Cart Class
23********************
Derek Jones8ede1a22011-10-05 13:34:52 -050024
25Initializing the Shopping Cart Class
26====================================
27
28.. important:: The Cart class utilizes CodeIgniter's :doc:`Session
29 Class <sessions>` to save the cart information to a database, so
30 before using the Cart class you must set up a database table as
31 indicated in the :doc:`Session Documentation <sessions>`, and set the
32 session preferences in your application/config/config.php file to
33 utilize a database.
34
35To initialize the Shopping Cart Class in your controller constructor,
36use the $this->load->library function::
37
38 $this->load->library('cart');
39
40Once loaded, the Cart object will be available using::
Derek Jones82b34e62013-07-21 23:07:20 -070041
Derek Jones8ede1a22011-10-05 13:34:52 -050042 $this->cart
43
44.. note:: The Cart Class will load and initialize the Session Class
45 automatically, so unless you are using sessions elsewhere in your
46 application, you do not need to load the Session class.
47
48Adding an Item to The Cart
49==========================
50
51To add an item to the shopping cart, simply pass an array with the
52product information to the $this->cart->insert() function, as shown
53below::
54
55 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -050056 'id' => 'sku_123ABC',
57 'qty' => 1,
58 'price' => 39.95,
59 'name' => 'T-Shirt',
60 'options' => array('Size' => 'L', 'Color' => 'Red')
61 );
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63 $this->cart->insert($data);
64
65.. important:: The first four array indexes above (id, qty, price, and
66 name) are **required**. If you omit any of them the data will not be
67 saved to the cart. The fifth index (options) is optional. It is intended
68 to be used in cases where your product has options associated with it.
69 Use an array for options, as shown above.
70
71The five reserved indexes are:
72
73- **id** - Each product in your store must have a unique identifier.
74 Typically this will be an "sku" or other such identifier.
75- **qty** - The quantity being purchased.
76- **price** - The price of the item.
77- **name** - The name of the item.
78- **options** - Any additional attributes that are needed to identify
79 the product. These must be passed via an array.
80
81In addition to the five indexes above, there are two reserved words:
82rowid and subtotal. These are used internally by the Cart class, so
83please do NOT use those words as index names when inserting data into
84the cart.
85
86Your array may contain additional data. Anything you include in your
87array will be stored in the session. However, it is best to standardize
88your data among all your products in order to make displaying the
89information in a table easier.
90
Ahmad Anbar576439f2014-02-13 06:16:31 +020091::
92
93 $data = array(
94 'id' => 'sku_123ABC',
95 'qty' => 1,
96 'price' => 39.95,
97 'name' => 'T-Shirt',
98 'coupon' => 'XMAS-50OFF'
99 );
100
101 $this->cart->insert($data);
102
Derek Jones8ede1a22011-10-05 13:34:52 -0500103The insert() method will return the $rowid if you successfully insert a
104single item.
105
106Adding Multiple Items to The Cart
107=================================
108
109By using a multi-dimensional array, as shown below, it is possible to
110add multiple products to the cart in one action. This is useful in cases
111where you wish to allow people to select from among several items on the
112same page.
113
114::
115
116 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -0500117 array(
118 'id' => 'sku_123ABC',
119 'qty' => 1,
120 'price' => 39.95,
121 'name' => 'T-Shirt',
122 'options' => array('Size' => 'L', 'Color' => 'Red')
123 ),
124 array(
125 'id' => 'sku_567ZYX',
126 'qty' => 1,
127 'price' => 9.95,
128 'name' => 'Coffee Mug'
129 ),
130 array(
131 'id' => 'sku_965QRS',
132 'qty' => 1,
133 'price' => 29.95,
134 'name' => 'Shot Glass'
135 )
136 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
138 $this->cart->insert($data);
139
140Displaying the Cart
141===================
142
143To display the cart you will create a :doc:`view
144file </general/views>` with code similar to the one shown below.
145
146Please note that this example uses the :doc:`form
147helper </helpers/form_helper>`.
148
149::
150
151 <?php echo form_open('path/to/controller/update/function'); ?>
152
153 <table cellpadding="6" cellspacing="1" style="width:100%" border="0">
154
155 <tr>
156 <th>QTY</th>
157 <th>Item Description</th>
158 <th style="text-align:right">Item Price</th>
159 <th style="text-align:right">Sub-Total</th>
160 </tr>
161
162 <?php $i = 1; ?>
163
164 <?php foreach ($this->cart->contents() as $items): ?>
165
166 <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
167
168 <tr>
169 <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
170 <td>
171 <?php echo $items['name']; ?>
172
173 <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
174
175 <p>
176 <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
177
178 <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
179
180 <?php endforeach; ?>
181 </p>
182
183 <?php endif; ?>
184
185 </td>
186 <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
187 <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
188 </tr>
189
190 <?php $i++; ?>
191
192 <?php endforeach; ?>
193
194 <tr>
195 <td colspan="2"> </td>
196 <td class="right"><strong>Total</strong></td>
197 <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
198 </tr>
199
200 </table>
201
202 <p><?php echo form_submit('', 'Update your Cart'); ?></p>
Derek Jones82b34e62013-07-21 23:07:20 -0700203
Derek Jones8ede1a22011-10-05 13:34:52 -0500204Updating The Cart
205=================
206
207To update the information in your cart, you must pass an array
208containing the Row ID and quantity to the $this->cart->update()
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200209function.
Derek Jones8ede1a22011-10-05 13:34:52 -0500210
211.. note:: If the quantity is set to zero, the item will be removed from
212 the cart.
213
214::
215
216 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -0500217 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
218 'qty' => 3
219 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
Derek Jones82b34e62013-07-21 23:07:20 -0700221 $this->cart->update($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500222
223 // Or a multi-dimensional array
224
225 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -0500226 array(
227 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
228 'qty' => 3
229 ),
230 array(
231 'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
232 'qty' => 4
233 ),
234 array(
235 'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
236 'qty' => 2
237 )
238 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500239
Derek Jones97f283d2011-10-05 16:17:25 -0500240 $this->cart->update($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500241
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200242You may also update any property you have previously
243defined when inserting the item such as options, price
244or other custom fields you defined.
245
246::
Ahmad Anbarc09b9532014-02-13 13:05:41 +0200247
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200248 $data = array(
249 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
250 'qty' => 1,
251 'price' => 49.95,
252 'coupon' => NULL
253 );
254
255 $this->cart->update($data);
256
Derek Jones8ede1a22011-10-05 13:34:52 -0500257What is a Row ID?
258*****************
259
260The row ID is a unique identifier that is
261generated by the cart code when an item is added to the cart. The reason
262a unique ID is created is so that identical products with different
263options can be managed by the cart.
264
265For example, let's say someone buys two identical t-shirts (same product
266ID), but in different sizes. The product ID (and other attributes) will
267be identical for both sizes because it's the same shirt. The only
268difference will be the size. The cart must therefore have a means of
269identifying this difference so that the two sizes of shirts can be
270managed independently. It does so by creating a unique "row ID" based on
271the product ID and any options associated with it.
272
273In nearly all cases, updating the cart will be something the user does
274via the "view cart" page, so as a developer, it is unlikely that you
Andrey Andreevba231aa2014-01-20 16:43:41 +0200275will ever have to concern yourself with the "row ID", other than making
Derek Jones8ede1a22011-10-05 13:34:52 -0500276sure your "view cart" page contains this information in a hidden form
277field, and making sure it gets passed to the update function when the
278update form is submitted. Please examine the construction of the "view
279cart" page above for more information.
280
281
Derek Jones82b34e62013-07-21 23:07:20 -0700282***************
283Class Reference
284***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500285
Derek Jones82b34e62013-07-21 23:07:20 -0700286.. class:: CI_Cart
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Derek Jones82b34e62013-07-21 23:07:20 -0700288 .. attribute:: $product_id_rules = '\.a-z0-9_-'
Derek Jones8ede1a22011-10-05 13:34:52 -0500289
Derek Jones82b34e62013-07-21 23:07:20 -0700290 These are the regular expression rules that we use to validate the product
291 ID - alpha-numeric, dashes, underscores, or periods by default
Derek Jones8ede1a22011-10-05 13:34:52 -0500292
Derek Jones82b34e62013-07-21 23:07:20 -0700293 .. attribute:: $product_name_rules = '\w \-\.\:'
Derek Jones8ede1a22011-10-05 13:34:52 -0500294
Derek Jones82b34e62013-07-21 23:07:20 -0700295 These are the regular expression rules that we use to validate the product ID and product name - alpha-numeric, dashes, underscores, colons or periods by
296 default
Andrew Seymourf75ec112011-12-14 09:36:39 +0000297
Derek Jones82b34e62013-07-21 23:07:20 -0700298 .. attribute:: $product_name_safe = TRUE
Andrew Seymourf75ec112011-12-14 09:36:39 +0000299
Derek Jones82b34e62013-07-21 23:07:20 -0700300 Whether or not to only allow safe product names. Default TRUE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
Derek Jones8ede1a22011-10-05 13:34:52 -0500302
Derek Jones82b34e62013-07-21 23:07:20 -0700303 .. method:: insert([$items = array()])
Derek Jones8ede1a22011-10-05 13:34:52 -0500304
Andrey Andreev28c2c972014-02-08 04:27:48 +0200305 :param array $items: Items to insert into the cart
306 :returns: TRUE on success, FALSE on failure
307 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500308
Derek Jones82b34e62013-07-21 23:07:20 -0700309 Insert items into the cart and save it to the session table. Returns TRUE
310 on success and FALSE on failure.
Derek Jones8ede1a22011-10-05 13:34:52 -0500311
Derek Jones8ede1a22011-10-05 13:34:52 -0500312
Derek Jones82b34e62013-07-21 23:07:20 -0700313 .. method:: update([$items = array()])
Andrey Andreevcdeee662012-10-25 17:29:52 +0300314
Andrey Andreev28c2c972014-02-08 04:27:48 +0200315 :param array $items: Items to update in the cart
316 :returns: TRUE on success, FALSE on failure
317 :rtype: bool
Andrey Andreevcdeee662012-10-25 17:29:52 +0300318
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200319 This method permits changing the properties of a given item.
Derek Jones82b34e62013-07-21 23:07:20 -0700320 Typically it is called from the "view cart" page if a user makes changes
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200321 to the quantity before checkout. That array must contain the rowid
322 and qty for each item.
Derek Jones8ede1a22011-10-05 13:34:52 -0500323
Derek Jones82b34e62013-07-21 23:07:20 -0700324 .. method:: remove($rowid)
Derek Jones8ede1a22011-10-05 13:34:52 -0500325
Andrey Andreev28c2c972014-02-08 04:27:48 +0200326 :param int $rowid: ID of the item to remove from the cart
327 :returns: TRUE on success, FALSE on failure
328 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500329
Derek Jones82b34e62013-07-21 23:07:20 -0700330 Allows you to remove an item from the shopping cart by passing it the
331 ``$rowid``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500332
Derek Jones82b34e62013-07-21 23:07:20 -0700333 .. method:: total()
334
Andrey Andreev28c2c972014-02-08 04:27:48 +0200335 :returns: Total amount
336 :rtype: int
Derek Jones82b34e62013-07-21 23:07:20 -0700337
338 Displays the total amount in the cart.
339
340
341 .. method:: total_items()
342
Andrey Andreev28c2c972014-02-08 04:27:48 +0200343 :returns: Total amount of items in the cart
344 :rtype: int
Derek Jones82b34e62013-07-21 23:07:20 -0700345
346 Displays the total number of items in the cart.
347
348
349 .. method:: contents([$newest_first = FALSE])
350
Andrey Andreev28c2c972014-02-08 04:27:48 +0200351 :param bool $newest_first: Whether to order the array with newest items first
352 :returns: An array of cart contents
353 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700354
355 Returns an array containing everything in the cart. You can sort the
356 order by which the array is returned by passing it TRUE where the contents
357 will be sorted from newest to oldest, otherwise it is sorted from oldest
358 to newest.
359
Derek Jones82b34e62013-07-21 23:07:20 -0700360 .. method:: get_item($row_id)
361
Andrey Andreev28c2c972014-02-08 04:27:48 +0200362 :param int $row_id: Row ID to retrieve
363 :returns: Array of item data
364 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700365
366 Returns an array containing data for the item matching the specified row
367 ID, or FALSE if no such item exists.
368
Derek Jones82b34e62013-07-21 23:07:20 -0700369 .. method:: has_options($row_id = '')
370
Andrey Andreev28c2c972014-02-08 04:27:48 +0200371 :param int $row_id: Row ID to inspect
372 :returns: TRUE if options exist, FALSE otherwise
373 :rtype: bool
Derek Jones82b34e62013-07-21 23:07:20 -0700374
375 Returns TRUE (boolean) if a particular row in the cart contains options.
Andrey Andreev28c2c972014-02-08 04:27:48 +0200376 This method is designed to be used in a loop with ``contents()``, since
Derek Jones82b34e62013-07-21 23:07:20 -0700377 you must pass the rowid to this function, as shown in the Displaying
378 the Cart example above.
379
Derek Jones82b34e62013-07-21 23:07:20 -0700380 .. method:: product_options([$row_id = ''])
381
Andrey Andreev28c2c972014-02-08 04:27:48 +0200382 :param int $row_id: Row ID
383 :returns: Array of product options
384 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700385
386 Returns an array of options for a particular product. This method is
Andrey Andreev28c2c972014-02-08 04:27:48 +0200387 designed to be used in a loop with ``contents()``, since you
Derek Jones82b34e62013-07-21 23:07:20 -0700388 must pass the rowid to this method, as shown in the Displaying the
389 Cart example above.
390
Derek Jones82b34e62013-07-21 23:07:20 -0700391 .. method:: destroy()
392
Andrey Andreev28c2c972014-02-08 04:27:48 +0200393 :rtype: void
Derek Jones82b34e62013-07-21 23:07:20 -0700394
395 Permits you to destroy the cart. This method will likely be called
396 when you are finished processing the customer's order.