summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/libraries/cart.rst
blob: 2384e92b914738b8c67f2556065eac164e4d2878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
###################
Shopping Cart Class
###################

The Cart Class permits items to be added to a session that stays active
while a user is browsing your site. 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.

Please note that the Cart Class ONLY provides the core "cart"
functionality. It does not provide shipping, credit card authorization,
or other processing components.

.. contents:: Page Contents

Initializing the Shopping Cart Class
====================================

.. important:: The Cart class utilizes CodeIgniter's :doc:`Session
	Class <sessions>` to save the cart information to a database, so
	before using the Cart class you must set up a database table as
	indicated in the :doc:`Session Documentation <sessions>`, and set the
	session preferences in your application/config/config.php file to
	utilize a database.

To initialize the Shopping Cart Class in your controller constructor,
use the $this->load->library function::

	$this->load->library('cart');

Once loaded, the Cart object will be available using::
 	
	$this->cart

.. note:: 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.

Adding an Item to The Cart
==========================

To add an item to the shopping cart, simply pass an array with the
product information to the $this->cart->insert() function, as shown
below::

	$data = array(
		'id'		=> 'sku_123ABC',
		'qty'	=> 1,
		'price'	=> 39.95,
		'name'	=> 'T-Shirt',
		'options'	=> array('Size' => 'L', 'Color' => 'Red')
	);

	$this->cart->insert($data);

.. important:: The first four array indexes above (id, qty, price, and
	name) are **required**. If you omit any of them the data will not be
	saved to the cart. The fifth index (options) is optional. It is intended
	to be used in cases where your product has options associated with it.
	Use an array for options, as shown above.

The five reserved indexes are:

-  **id** - Each product in your store must have a unique identifier.
   Typically this will be an "sku" or other such identifier.
-  **qty** - The quantity being purchased.
-  **price** - The price of the item.
-  **name** - The name of the item.
-  **options** - Any additional attributes that are needed to identify
   the product. These must be passed via an array.

In addition to the five indexes above, there are two reserved words:
rowid and subtotal. These are used internally by the Cart class, so
please do NOT use those words as index names when inserting data into
the cart.

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 all your products in order to make displaying the
information in a table easier.

The insert() method will return the $rowid if you successfully insert a
single item.

Adding Multiple Items to The Cart
=================================

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 people to select from among several items on the
same page.

::

	$data = array(
		 array(
			'id'	  => 'sku_123ABC',
			'qty'	 => 1,
			'price'   => 39.95,
			'name'	=> 'T-Shirt',
			'options' => array('Size' => 'L', 'Color' => 'Red')
		),
		 array(
			'id'	  => 'sku_567ZYX',
			'qty'	 => 1,
			'price'   => 9.95,
			'name'	=> 'Coffee Mug'
		),
		 array(
			'id'	  => 'sku_965QRS',
			'qty'	 => 1,
			'price'   => 29.95,
			'name'	=> 'Shot Glass'
		)
	);

	$this->cart->insert($data);

Displaying the Cart
===================

To display the cart you will create a :doc:`view
file </general/views>` with code similar to the one shown below.

Please note that this example uses the :doc:`form
helper </helpers/form_helper>`.

::

	<?php echo form_open('path/to/controller/update/function'); ?>

	<table cellpadding="6" cellspacing="1" style="width:100%" border="0">

	<tr>
	  <th>QTY</th>
	  <th>Item Description</th>
	  <th style="text-align:right">Item Price</th>
	  <th style="text-align:right">Sub-Total</th>
	</tr>

	<?php $i = 1; ?>

	<?php foreach ($this->cart->contents() as $items): ?>

		<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

		<tr>
		  <td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
		  <td>
			<?php echo $items['name']; ?>

				<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>

					<p>
						<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>

							<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />

						<?php endforeach; ?>
					</p>

				<?php endif; ?>

		  </td>
		  <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
		  <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
		</tr>

	<?php $i++; ?>

	<?php endforeach; ?>

	<tr>
	  <td colspan="2"> </td>
	  <td class="right"><strong>Total</strong></td>
	  <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
	</tr>

	</table>

	<p><?php echo form_submit('', 'Update your Cart'); ?></p>
	
Updating The Cart
=================

To update the information in your cart, you must pass an array
containing the Row ID and quantity to the $this->cart->update()
function:

.. note:: If the quantity is set to zero, the item will be removed from
	the cart.

::

	$data = array(
		'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
		'qty'   => 3
	);

	$this->cart->update($data); 

	// Or a multi-dimensional array

	$data = array(
		array(
		   'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',
		   'qty'     => 3
		),
		array(
		   'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',
		   'qty'     => 4
		),
		array(
		   'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
		   'qty'     => 2
		)
	);

	$this->cart->update($data); 

What is a Row ID?
*****************

The row ID is a unique identifier that is
generated by the cart code when an item is added to the cart. The reason
a unique ID is created is so that identical products with different
options can be managed by the cart.

For example, let's say someone buys two identical t-shirts (same product
ID), but in different sizes. The product ID (and other attributes) will
be identical for both sizes because it's the same shirt. The only
difference will be the size. The cart must therefore have a means of
identifying this difference so that the two sizes of shirts can be
managed independently. It does so by creating a unique "row ID" based on
the product ID and any options associated with it.

In nearly all cases, updating the cart will be something the user does
via the "view cart" page, so as a developer, it is unlikely that you
will ever have to concern yourself with the "row ID", other then making
sure your "view cart" page contains this information in a hidden form
field, and making sure it gets passed to the update function when the
update form is submitted. Please examine the construction of the "view
cart" page above for more information.


Function Reference
==================

$this->cart->insert();
**********************

Permits you to add items to the shopping cart, as outlined above.

$this->cart->update();
**********************

Permits you to update items in the shopping cart, as outlined above.

$this->cart->total();
*********************

Displays the total amount in the cart.

$this->cart->total_items();
****************************

Displays the total number of items in the cart.

$this->cart->contents();
************************

Returns an array containing everything in the cart.

$this->cart->has_options(rowid);
*********************************

Returns TRUE (boolean) if a particular row in the cart contains options.
This function is designed to be used in a loop with
$this->cart->contents(), since you must pass the rowid to this function,
as shown in the Displaying the Cart example above.

$this->cart->product_options(rowid);
*************************************

Returns an array of options for a particular product. This function is
designed to be used in a loop with $this->cart->contents(), since you
must pass the rowid to this function, as shown in the Displaying the
Cart example above.

$this->cart->destroy();
***********************

Permits you to destroy the cart. This function will likely be called
when you are finished processing the customer's order.