blob: be343320db80e065be2d7249cb9514dd7a6ae30d [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
Andrey Andreeve3d66af2015-01-19 14:51:37 +020010.. important:: The Cart library is DEPRECATED and should not be used.
Andrey Andreev21c3c222014-12-04 12:10:00 +020011 It is currently only kept for backwards compatibility.
12
Derek Jones8ede1a22011-10-05 13:34:52 -050013Please note that the Cart Class ONLY provides the core "cart"
14functionality. It does not provide shipping, credit card authorization,
15or other processing components.
16
Derek Jones82b34e62013-07-21 23:07:20 -070017.. contents::
18 :local:
19
20.. raw:: html
21
22 <div class="custom-index container"></div>
23
24********************
25Using the Cart Class
26********************
Derek Jones8ede1a22011-10-05 13:34:52 -050027
28Initializing the Shopping Cart Class
29====================================
30
31.. important:: The Cart class utilizes CodeIgniter's :doc:`Session
32 Class <sessions>` to save the cart information to a database, so
33 before using the Cart class you must set up a database table as
34 indicated in the :doc:`Session Documentation <sessions>`, and set the
35 session preferences in your application/config/config.php file to
36 utilize a database.
37
38To initialize the Shopping Cart Class in your controller constructor,
Andrey Andreev0bd390c2014-02-13 14:26:50 +020039use the ``$this->load->library()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41 $this->load->library('cart');
42
43Once loaded, the Cart object will be available using::
Derek Jones82b34e62013-07-21 23:07:20 -070044
Derek Jones8ede1a22011-10-05 13:34:52 -050045 $this->cart
46
47.. note:: The Cart Class will load and initialize the Session Class
48 automatically, so unless you are using sessions elsewhere in your
49 application, you do not need to load the Session class.
50
51Adding an Item to The Cart
52==========================
53
54To add an item to the shopping cart, simply pass an array with the
Andrey Andreev0bd390c2014-02-13 14:26:50 +020055product information to the ``$this->cart->insert()`` method, as shown
Derek Jones8ede1a22011-10-05 13:34:52 -050056below::
57
58 $data = array(
Andrey Andreev0bd390c2014-02-13 14:26:50 +020059 'id' => 'sku_123ABC',
60 'qty' => 1,
61 'price' => 39.95,
62 'name' => 'T-Shirt',
63 'options' => array('Size' => 'L', 'Color' => 'Red')
64 );
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66 $this->cart->insert($data);
67
68.. important:: The first four array indexes above (id, qty, price, and
69 name) are **required**. If you omit any of them the data will not be
70 saved to the cart. The fifth index (options) is optional. It is intended
71 to be used in cases where your product has options associated with it.
72 Use an array for options, as shown above.
73
74The five reserved indexes are:
75
76- **id** - Each product in your store must have a unique identifier.
77 Typically this will be an "sku" or other such identifier.
78- **qty** - The quantity being purchased.
79- **price** - The price of the item.
80- **name** - The name of the item.
81- **options** - Any additional attributes that are needed to identify
82 the product. These must be passed via an array.
83
84In addition to the five indexes above, there are two reserved words:
85rowid and subtotal. These are used internally by the Cart class, so
86please do NOT use those words as index names when inserting data into
87the cart.
88
89Your array may contain additional data. Anything you include in your
90array will be stored in the session. However, it is best to standardize
91your data among all your products in order to make displaying the
92information in a table easier.
93
Ahmad Anbar576439f2014-02-13 06:16:31 +020094::
95
96 $data = array(
Andrey Andreev0bd390c2014-02-13 14:26:50 +020097 'id' => 'sku_123ABC',
98 'qty' => 1,
99 'price' => 39.95,
100 'name' => 'T-Shirt',
101 'coupon' => 'XMAS-50OFF'
102 );
Ahmad Anbar576439f2014-02-13 06:16:31 +0200103
104 $this->cart->insert($data);
105
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200106The ``insert()`` method will return the $rowid if you successfully insert a
Derek Jones8ede1a22011-10-05 13:34:52 -0500107single item.
108
109Adding Multiple Items to The Cart
110=================================
111
112By using a multi-dimensional array, as shown below, it is possible to
113add multiple products to the cart in one action. This is useful in cases
114where you wish to allow people to select from among several items on the
115same page.
116
117::
118
119 $data = array(
Andrey Andreev81f03672014-02-13 17:57:41 +0200120 array(
121 'id' => 'sku_123ABC',
122 'qty' => 1,
123 'price' => 39.95,
124 'name' => 'T-Shirt',
125 'options' => array('Size' => 'L', 'Color' => 'Red')
126 ),
127 array(
128 'id' => 'sku_567ZYX',
129 'qty' => 1,
130 'price' => 9.95,
131 'name' => 'Coffee Mug'
132 ),
133 array(
134 'id' => 'sku_965QRS',
135 'qty' => 1,
136 'price' => 29.95,
137 'name' => 'Shot Glass'
138 )
139 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141 $this->cart->insert($data);
142
143Displaying the Cart
144===================
145
146To display the cart you will create a :doc:`view
147file </general/views>` with code similar to the one shown below.
148
149Please note that this example uses the :doc:`form
150helper </helpers/form_helper>`.
151
152::
153
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200154 <?php echo form_open('path/to/controller/update/method'); ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
156 <table cellpadding="6" cellspacing="1" style="width:100%" border="0">
157
158 <tr>
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200159 <th>QTY</th>
160 <th>Item Description</th>
161 <th style="text-align:right">Item Price</th>
162 <th style="text-align:right">Sub-Total</th>
Derek Jones8ede1a22011-10-05 13:34:52 -0500163 </tr>
164
165 <?php $i = 1; ?>
166
167 <?php foreach ($this->cart->contents() as $items): ?>
168
169 <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
170
171 <tr>
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200172 <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
173 <td>
174 <?php echo $items['name']; ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
176 <?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
177
178 <p>
179 <?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
180
181 <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
182
183 <?php endforeach; ?>
184 </p>
185
186 <?php endif; ?>
187
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200188 </td>
189 <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
190 <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500191 </tr>
192
193 <?php $i++; ?>
194
195 <?php endforeach; ?>
196
197 <tr>
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200198 <td colspan="2"> </td>
199 <td class="right"><strong>Total</strong></td>
200 <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500201 </tr>
202
203 </table>
204
205 <p><?php echo form_submit('', 'Update your Cart'); ?></p>
Derek Jones82b34e62013-07-21 23:07:20 -0700206
Derek Jones8ede1a22011-10-05 13:34:52 -0500207Updating The Cart
208=================
209
210To update the information in your cart, you must pass an array
Ahmad Anbar2702a3b2014-03-14 16:53:44 +0200211containing the Row ID and one or more pre-defined properties to the
212``$this->cart->update()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214.. note:: If the quantity is set to zero, the item will be removed from
215 the cart.
216
217::
218
219 $data = array(
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200220 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
221 'qty' => 3
222 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500223
Derek Jones82b34e62013-07-21 23:07:20 -0700224 $this->cart->update($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
226 // Or a multi-dimensional array
227
228 $data = array(
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200229 array(
230 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
231 'qty' => 3
232 ),
233 array(
234 'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
235 'qty' => 4
236 ),
237 array(
238 'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
239 'qty' => 2
240 )
241 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
Derek Jones97f283d2011-10-05 16:17:25 -0500243 $this->cart->update($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200245You may also update any property you have previously defined when
246inserting the item such as options, price or other custom fields.
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200247
248::
Ahmad Anbarc09b9532014-02-13 13:05:41 +0200249
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200250 $data = array(
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200251 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
252 'qty' => 1,
253 'price' => 49.95,
254 'coupon' => NULL
255 );
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200256
257 $this->cart->update($data);
258
Derek Jones8ede1a22011-10-05 13:34:52 -0500259What is a Row ID?
260*****************
261
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200262The row ID is a unique identifier that is generated by the cart code
263when an item is added to the cart. The reason a unique ID is created
264is so that identical products with different options can be managed
265by the cart.
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
267For example, let's say someone buys two identical t-shirts (same product
268ID), but in different sizes. The product ID (and other attributes) will
269be identical for both sizes because it's the same shirt. The only
270difference will be the size. The cart must therefore have a means of
271identifying this difference so that the two sizes of shirts can be
272managed independently. It does so by creating a unique "row ID" based on
273the product ID and any options associated with it.
274
275In nearly all cases, updating the cart will be something the user does
276via the "view cart" page, so as a developer, it is unlikely that you
Andrey Andreevba231aa2014-01-20 16:43:41 +0200277will ever have to concern yourself with the "row ID", other than making
Derek Jones8ede1a22011-10-05 13:34:52 -0500278sure your "view cart" page contains this information in a hidden form
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200279field, and making sure it gets passed to the ``update()`` method when
280the update form is submitted. Please examine the construction of the
281"view cart" page above for more information.
Derek Jones8ede1a22011-10-05 13:34:52 -0500282
283
Derek Jones82b34e62013-07-21 23:07:20 -0700284***************
285Class Reference
286***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200288.. php:class:: CI_Cart
Derek Jones8ede1a22011-10-05 13:34:52 -0500289
Derek Jones82b34e62013-07-21 23:07:20 -0700290 .. attribute:: $product_id_rules = '\.a-z0-9_-'
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Derek Jones82b34e62013-07-21 23:07:20 -0700292 These are the regular expression rules that we use to validate the product
293 ID - alpha-numeric, dashes, underscores, or periods by default
Derek Jones8ede1a22011-10-05 13:34:52 -0500294
Derek Jones82b34e62013-07-21 23:07:20 -0700295 .. attribute:: $product_name_rules = '\w \-\.\:'
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
Derek Jones82b34e62013-07-21 23:07:20 -0700297 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
298 default
Andrew Seymourf75ec112011-12-14 09:36:39 +0000299
Derek Jones82b34e62013-07-21 23:07:20 -0700300 .. attribute:: $product_name_safe = TRUE
Andrew Seymourf75ec112011-12-14 09:36:39 +0000301
Derek Jones82b34e62013-07-21 23:07:20 -0700302 Whether or not to only allow safe product names. Default TRUE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Derek Jones8ede1a22011-10-05 13:34:52 -0500304
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200305 .. php:method:: insert([$items = array()])
Derek Jones8ede1a22011-10-05 13:34:52 -0500306
Andrey Andreev28c2c972014-02-08 04:27:48 +0200307 :param array $items: Items to insert into the cart
308 :returns: TRUE on success, FALSE on failure
309 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
Derek Jones82b34e62013-07-21 23:07:20 -0700311 Insert items into the cart and save it to the session table. Returns TRUE
312 on success and FALSE on failure.
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200315 .. php:method:: update([$items = array()])
Andrey Andreevcdeee662012-10-25 17:29:52 +0300316
Andrey Andreev28c2c972014-02-08 04:27:48 +0200317 :param array $items: Items to update in the cart
318 :returns: TRUE on success, FALSE on failure
319 :rtype: bool
Andrey Andreevcdeee662012-10-25 17:29:52 +0300320
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200321 This method permits changing the properties of a given item.
Derek Jones82b34e62013-07-21 23:07:20 -0700322 Typically it is called from the "view cart" page if a user makes changes
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200323 to the quantity before checkout. That array must contain the rowid
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200324 for each item.
Derek Jones8ede1a22011-10-05 13:34:52 -0500325
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200326 .. php:method:: remove($rowid)
Derek Jones8ede1a22011-10-05 13:34:52 -0500327
Andrey Andreev28c2c972014-02-08 04:27:48 +0200328 :param int $rowid: ID of the item to remove from the cart
329 :returns: TRUE on success, FALSE on failure
330 :rtype: bool
Derek Jones8ede1a22011-10-05 13:34:52 -0500331
Derek Jones82b34e62013-07-21 23:07:20 -0700332 Allows you to remove an item from the shopping cart by passing it the
333 ``$rowid``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200335 .. php:method:: total()
Derek Jones82b34e62013-07-21 23:07:20 -0700336
Andrey Andreev28c2c972014-02-08 04:27:48 +0200337 :returns: Total amount
338 :rtype: int
Derek Jones82b34e62013-07-21 23:07:20 -0700339
340 Displays the total amount in the cart.
341
342
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200343 .. php:method:: total_items()
Derek Jones82b34e62013-07-21 23:07:20 -0700344
Andrey Andreev28c2c972014-02-08 04:27:48 +0200345 :returns: Total amount of items in the cart
346 :rtype: int
Derek Jones82b34e62013-07-21 23:07:20 -0700347
348 Displays the total number of items in the cart.
349
350
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200351 .. php:method:: contents([$newest_first = FALSE])
Derek Jones82b34e62013-07-21 23:07:20 -0700352
Andrey Andreev28c2c972014-02-08 04:27:48 +0200353 :param bool $newest_first: Whether to order the array with newest items first
354 :returns: An array of cart contents
355 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700356
357 Returns an array containing everything in the cart. You can sort the
358 order by which the array is returned by passing it TRUE where the contents
359 will be sorted from newest to oldest, otherwise it is sorted from oldest
360 to newest.
361
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200362 .. php:method:: get_item($row_id)
Derek Jones82b34e62013-07-21 23:07:20 -0700363
Andrey Andreev28c2c972014-02-08 04:27:48 +0200364 :param int $row_id: Row ID to retrieve
365 :returns: Array of item data
366 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700367
368 Returns an array containing data for the item matching the specified row
369 ID, or FALSE if no such item exists.
370
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200371 .. php:method:: has_options($row_id = '')
Derek Jones82b34e62013-07-21 23:07:20 -0700372
Andrey Andreev28c2c972014-02-08 04:27:48 +0200373 :param int $row_id: Row ID to inspect
374 :returns: TRUE if options exist, FALSE otherwise
375 :rtype: bool
Derek Jones82b34e62013-07-21 23:07:20 -0700376
377 Returns TRUE (boolean) if a particular row in the cart contains options.
Andrey Andreev28c2c972014-02-08 04:27:48 +0200378 This method is designed to be used in a loop with ``contents()``, since
Andrey Andreev0bd390c2014-02-13 14:26:50 +0200379 you must pass the rowid to this method, as shown in the Displaying
Derek Jones82b34e62013-07-21 23:07:20 -0700380 the Cart example above.
381
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200382 .. php:method:: product_options([$row_id = ''])
Derek Jones82b34e62013-07-21 23:07:20 -0700383
Andrey Andreev28c2c972014-02-08 04:27:48 +0200384 :param int $row_id: Row ID
385 :returns: Array of product options
386 :rtype: array
Derek Jones82b34e62013-07-21 23:07:20 -0700387
388 Returns an array of options for a particular product. This method is
Andrey Andreev28c2c972014-02-08 04:27:48 +0200389 designed to be used in a loop with ``contents()``, since you
Derek Jones82b34e62013-07-21 23:07:20 -0700390 must pass the rowid to this method, as shown in the Displaying the
391 Cart example above.
392
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200393 .. php:method:: destroy()
Derek Jones82b34e62013-07-21 23:07:20 -0700394
Andrey Andreev28c2c972014-02-08 04:27:48 +0200395 :rtype: void
Derek Jones82b34e62013-07-21 23:07:20 -0700396
397 Permits you to destroy the cart. This method will likely be called
398 when you are finished processing the customer's order.