blob: a1e042ef2b2c0fc5d9cd248df161bb9ae3150d13 [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
14.. contents:: Page Contents
15
16Initializing 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
26To initialize the Shopping Cart Class in your controller constructor,
27use the $this->load->library function::
28
29 $this->load->library('cart');
30
31Once 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
39Adding an Item to The Cart
40==========================
41
42To add an item to the shopping cart, simply pass an array with the
43product information to the $this->cart->insert() function, as shown
44below::
45
46 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -050047 'id' => 'sku_123ABC',
48 'qty' => 1,
49 'price' => 39.95,
50 'name' => 'T-Shirt',
51 'options' => array('Size' => 'L', 'Color' => 'Red')
52 );
Derek Jones8ede1a22011-10-05 13:34:52 -050053
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
62The 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
72In addition to the five indexes above, there are two reserved words:
73rowid and subtotal. These are used internally by the Cart class, so
74please do NOT use those words as index names when inserting data into
75the cart.
76
77Your array may contain additional data. Anything you include in your
78array will be stored in the session. However, it is best to standardize
79your data among all your products in order to make displaying the
80information in a table easier.
81
82The insert() method will return the $rowid if you successfully insert a
83single item.
84
85Adding Multiple Items to The Cart
86=================================
87
88By using a multi-dimensional array, as shown below, it is possible to
89add multiple products to the cart in one action. This is useful in cases
90where you wish to allow people to select from among several items on the
91same page.
92
93::
94
95 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -050096 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 Jones8ede1a22011-10-05 13:34:52 -0500116
117 $this->cart->insert($data);
118
119Displaying the Cart
120===================
121
122To display the cart you will create a :doc:`view
123file </general/views>` with code similar to the one shown below.
124
125Please note that this example uses the :doc:`form
126helper </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
183Updating The Cart
184=================
185
186To update the information in your cart, you must pass an array
187containing the Row ID and quantity to the $this->cart->update()
188function:
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 Jones97f283d2011-10-05 16:17:25 -0500196 'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
197 'qty' => 3
198 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
200 $this->cart->update($data);
201
202 // Or a multi-dimensional array
203
204 $data = array(
Derek Jones97f283d2011-10-05 16:17:25 -0500205 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 Jones8ede1a22011-10-05 13:34:52 -0500218
Derek Jones97f283d2011-10-05 16:17:25 -0500219 $this->cart->update($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
221What is a Row ID?
222*****************
223
224The row ID is a unique identifier that is
225generated by the cart code when an item is added to the cart. The reason
226a unique ID is created is so that identical products with different
227options can be managed by the cart.
228
229For example, let's say someone buys two identical t-shirts (same product
230ID), but in different sizes. The product ID (and other attributes) will
231be identical for both sizes because it's the same shirt. The only
232difference will be the size. The cart must therefore have a means of
233identifying this difference so that the two sizes of shirts can be
234managed independently. It does so by creating a unique "row ID" based on
235the product ID and any options associated with it.
236
237In nearly all cases, updating the cart will be something the user does
238via the "view cart" page, so as a developer, it is unlikely that you
239will ever have to concern yourself with the "row ID", other then making
240sure your "view cart" page contains this information in a hidden form
241field, and making sure it gets passed to the update function when the
242update form is submitted. Please examine the construction of the "view
243cart" page above for more information.
244
245
246Function Reference
247==================
248
249$this->cart->insert();
250**********************
251
252Permits you to add items to the shopping cart, as outlined above.
253
254$this->cart->update();
255**********************
256
257Permits you to update items in the shopping cart, as outlined above.
258
259$this->cart->total();
260*********************
261
262Displays the total amount in the cart.
263
264$this->cart->total_items();
265****************************
266
267Displays the total number of items in the cart.
268
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000269$this->cart->contents(boolean);
Derek Jones8ede1a22011-10-05 13:34:52 -0500270************************
271
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000272Returns an array containing everything in the cart. You can sort the order,
273by which this is returned by passing it "true" where the contents will be sorted
274from newest to oldest, by leaving this function blank, you'll automatically just get
275first added to the basket to last added to the basket.
Derek Jones8ede1a22011-10-05 13:34:52 -0500276
277$this->cart->has_options(rowid);
278*********************************
279
280Returns TRUE (boolean) if a particular row in the cart contains options.
281This function is designed to be used in a loop with
282$this->cart->contents(), since you must pass the rowid to this function,
283as shown in the Displaying the Cart example above.
284
285$this->cart->product_options(rowid);
286*************************************
287
288Returns an array of options for a particular product. This function is
289designed to be used in a loop with $this->cart->contents(), since you
290must pass the rowid to this function, as shown in the Displaying the
291Cart example above.
292
293$this->cart->destroy();
294***********************
295
296Permits you to destroy the cart. This function will likely be called
297when you are finished processing the customer's order.