blob: 7f6cdf5ddf5e852e73a474f128bf755b1bb00539 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Rick Ellis98783322009-02-17 02:29:44 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Rick Ellis98783322009-02-17 02:29:44 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Rick Ellis98783322009-02-17 02:29:44 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Rick Ellis98783322009-02-17 02:29:44 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Shopping Cart Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Shopping Cart
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Rick Ellis98783322009-02-17 02:29:44 +000037 * @link http://codeigniter.com/user_guide/libraries/cart.html
38 */
39class CI_Cart {
40
41 // These are the regular expression rules that we use to validate the product ID and product name
42 var $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
43 var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
Andrew Seymour2e5bda32011-12-13 11:40:15 +000044 var $product_name_safe = true; // only allow safe product names
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Jones37f4b9c2011-07-01 17:56:50 -050046 // Private variables. Do not change!
Rick Ellis98783322009-02-17 02:29:44 +000047 var $CI;
Rick Ellisdf39d512009-02-24 22:29:37 +000048 var $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000049
50
51 /**
52 * Shopping Class Constructor
53 *
54 * The constructor loads the Session class, used to store the shopping cart contents.
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Greg Akera9263282010-11-10 15:26:43 -060056 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020057 {
Rick Ellis98783322009-02-17 02:29:44 +000058 // Set the super object to a local variable for use later
59 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jones37f4b9c2011-07-01 17:56:50 -050061 // Are any config settings being passed manually? If so, set them
Rick Ellis98783322009-02-17 02:29:44 +000062 $config = array();
63 if (count($params) > 0)
64 {
65 foreach ($params as $key => $val)
66 {
67 $config[$key] = $val;
68 }
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Rick Ellis98783322009-02-17 02:29:44 +000071 // Load the Sessions class
72 $this->CI->load->library('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +020073
Rick Ellis98783322009-02-17 02:29:44 +000074 // Grab the shopping cart array from the session table, if it exists
75 if ($this->CI->session->userdata('cart_contents') !== FALSE)
76 {
77 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
78 }
79 else
80 {
81 // No cart exists so we'll set some base values
Barry Mienydd671972010-10-04 16:33:58 +020082 $this->_cart_contents['cart_total'] = 0;
83 $this->_cart_contents['total_items'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +000084 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Rick Ellis98783322009-02-17 02:29:44 +000086 log_message('debug', "Cart Class Initialized");
87 }
88
89 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020090
Rick Ellis98783322009-02-17 02:29:44 +000091 /**
92 * Insert items into the cart and save it to the session table
93 *
94 * @access public
95 * @param array
96 * @return bool
97 */
98 function insert($items = array())
99 {
100 // Was any cart data passed? No? Bah...
101 if ( ! is_array($items) OR count($items) == 0)
102 {
103 log_message('error', 'The insert method must be passed an array containing data.');
104 return FALSE;
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
107 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +0000108 // or multiple products using a multi-dimensional one. The way we
109 // determine the array type is by looking for a required array key named "id"
110 // at the top level. If it's not found, we will assume it's a multi-dimensional array.
Barry Mienydd671972010-10-04 16:33:58 +0200111
112 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000113 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +0200114 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600115 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000116 {
117 $save_cart = TRUE;
118 }
119 }
120 else
121 {
122 foreach ($items as $val)
123 {
124 if (is_array($val) AND isset($val['id']))
125 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600126 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000127 {
128 $save_cart = TRUE;
129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130 }
Rick Ellis98783322009-02-17 02:29:44 +0000131 }
132 }
133
134 // Save the cart data if the insert was successful
135 if ($save_cart == TRUE)
136 {
137 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600138 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000139 }
140
141 return FALSE;
142 }
143
144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145
Rick Ellis98783322009-02-17 02:29:44 +0000146 /**
147 * Insert
148 *
149 * @access private
150 * @param array
151 * @return bool
152 */
153 function _insert($items = array())
154 {
155 // Was any cart data passed? No? Bah...
156 if ( ! is_array($items) OR count($items) == 0)
157 {
158 log_message('error', 'The insert method must be passed an array containing data.');
159 return FALSE;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Rick Ellis98783322009-02-17 02:29:44 +0000162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones37f4b9c2011-07-01 17:56:50 -0500164 // Does the $items array contain an id, quantity, price, and name? These are required
Rick Ellis98783322009-02-17 02:29:44 +0000165 if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
166 {
167 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
168 return FALSE;
169 }
170
171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jones37f4b9c2011-07-01 17:56:50 -0500173 // Prep the quantity. It can only be a number. Duh...
Rick Ellis98783322009-02-17 02:29:44 +0000174 $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
175 // Trim any leading zeros
176 $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));
177
178 // If the quantity is zero or blank there's nothing for us to do
179 if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
180 {
181 return FALSE;
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Rick Ellis98783322009-02-17 02:29:44 +0000184 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200185
Rick Ellis98783322009-02-17 02:29:44 +0000186 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
187 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
188 // Note: These can be user-specified by setting the $this->product_id_rules variable.
189 if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
190 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500191 log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');
Rick Ellis98783322009-02-17 02:29:44 +0000192 return FALSE;
193 }
194
195 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200196
Rick Ellis98783322009-02-17 02:29:44 +0000197 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
198 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Andrew Seymour3dd66632011-12-13 15:50:52 +0000199 if ( $this->product_name_safe && ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000200 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000201 log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
202 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000203 }
204
205 // --------------------------------------------------------------------
206
Derek Jones37f4b9c2011-07-01 17:56:50 -0500207 // Prep the price. Remove anything that isn't a number or decimal point.
Rick Ellis98783322009-02-17 02:29:44 +0000208 $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
209 // Trim any leading zeros
210 $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
Barry Mienydd671972010-10-04 16:33:58 +0200211
Rick Ellis98783322009-02-17 02:29:44 +0000212 // Is the price a valid number?
213 if ( ! is_numeric($items['price']))
214 {
215 log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
216 return FALSE;
217 }
218
219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Rick Ellis98783322009-02-17 02:29:44 +0000221 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200222 // Every time something is added to the cart it is stored in the master cart array.
223 // Each row in the cart array, however, must have a unique index that identifies not only
224 // a particular product, but makes it possible to store identical products with different options.
225 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500226 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000227 // both sizes because it's the same shirt. The only difference will be the size.
228 // Internally, we need to treat identical submissions, but with different options, as a unique product.
229 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
230 // This becomes the unique "row ID"
231 if (isset($items['options']) AND count($items['options']) > 0)
232 {
233 $rowid = md5($items['id'].implode('', $items['options']));
234 }
235 else
236 {
237 // No options were submitted so we simply MD5 the product ID.
238 // Technically, we don't need to MD5 the ID in this case, but it makes
239 // sense to standardize the format of array indexes for both conditions
240 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200241 }
Rick Ellis98783322009-02-17 02:29:44 +0000242
243 // --------------------------------------------------------------------
244
245 // Now that we have our unique "row ID", we'll add our cart items to the master array
Andrew Seymour2e5bda32011-12-13 11:40:15 +0000246 // grab quantity if it's already there and add it on
Andrew Seymour17aebce2011-12-13 16:57:13 +0000247 if (isset($this->_cart_contents[$rowid]['qty']))
Andrew Seymour2e5bda32011-12-13 11:40:15 +0000248 {
249 // set our old quantity
250 $old_quantity = (int)$this->_cart_contents[$rowid]['qty'];
251 }
252 else
253 {
254 // we have no old quantity but - we don't want to throw an error
255 $old_quantity = 0;
256 }
257
Rick Ellis98783322009-02-17 02:29:44 +0000258 // let's unset this first, just to make sure our index contains only the data from this submission
Barry Mienydd671972010-10-04 16:33:58 +0200259 unset($this->_cart_contents[$rowid]);
260
Rick Ellis98783322009-02-17 02:29:44 +0000261 // Create a new index with our new row ID
262 $this->_cart_contents[$rowid]['rowid'] = $rowid;
Barry Mienydd671972010-10-04 16:33:58 +0200263
264 // And add the new items to the cart array
Rick Ellis98783322009-02-17 02:29:44 +0000265 foreach ($items as $key => $val)
266 {
267 $this->_cart_contents[$rowid][$key] = $val;
268 }
Andrew Seymour2e5bda32011-12-13 11:40:15 +0000269
270 // add old quantity back in
271 $this->_cart_contents[$rowid]['qty'] = ($this->_cart_contents[$rowid]['qty'] + $old_quantity);
272
Rick Ellis98783322009-02-17 02:29:44 +0000273 // Woot!
Phil Sturgeon90910512011-07-20 10:07:40 -0600274 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000275 }
276
277 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200278
Rick Ellis98783322009-02-17 02:29:44 +0000279 /**
280 * Update the cart
281 *
Barry Mienydd671972010-10-04 16:33:58 +0200282 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000283 * Typically it is called from the "view cart" page if a user makes
284 * changes to the quantity before checkout. That array must contain the
285 * product ID and quantity for each item.
286 *
287 * @access public
288 * @param array
289 * @param string
290 * @return bool
291 */
292 function update($items = array())
293 {
294 // Was any cart data passed?
295 if ( ! is_array($items) OR count($items) == 0)
296 {
297 return FALSE;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
300 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500301 // or multiple products using a multi-dimensional one. The way we
Rick Ellis98783322009-02-17 02:29:44 +0000302 // determine the array type is by looking for a required array key named "id".
303 // If it's not found we assume it's a multi-dimensional array
304 $save_cart = FALSE;
305 if (isset($items['rowid']) AND isset($items['qty']))
306 {
307 if ($this->_update($items) == TRUE)
308 {
309 $save_cart = TRUE;
310 }
311 }
312 else
313 {
314 foreach ($items as $val)
315 {
316 if (is_array($val) AND isset($val['rowid']) AND isset($val['qty']))
317 {
318 if ($this->_update($val) == TRUE)
319 {
320 $save_cart = TRUE;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322 }
Rick Ellis98783322009-02-17 02:29:44 +0000323 }
324 }
325
326 // Save the cart data if the insert was successful
327 if ($save_cart == TRUE)
328 {
329 $this->_save_cart();
330 return TRUE;
331 }
332
333 return FALSE;
334 }
335
336 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200337
Rick Ellis98783322009-02-17 02:29:44 +0000338 /**
339 * Update the cart
340 *
Barry Mienydd671972010-10-04 16:33:58 +0200341 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000342 * Typically it is called from the "view cart" page if a user makes
343 * changes to the quantity before checkout. That array must contain the
344 * product ID and quantity for each item.
345 *
346 * @access private
347 * @param array
348 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Rick Ellis98783322009-02-17 02:29:44 +0000350 function _update($items = array())
351 {
352 // Without these array indexes there is nothing we can do
353 if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))
354 {
355 return FALSE;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Rick Ellis98783322009-02-17 02:29:44 +0000358 // Prep the quantity
359 $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
360
361 // Is the quantity a number?
362 if ( ! is_numeric($items['qty']))
363 {
364 return FALSE;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Rick Ellis98783322009-02-17 02:29:44 +0000367 // Is the new quantity different than what is already saved in the cart?
368 // If it's the same there's nothing to do
369 if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])
370 {
371 return FALSE;
372 }
373
Derek Jones37f4b9c2011-07-01 17:56:50 -0500374 // Is the quantity zero? If so we will remove the item from the cart.
Rick Ellis98783322009-02-17 02:29:44 +0000375 // If the quantity is greater than zero we are updating
376 if ($items['qty'] == 0)
377 {
Barry Mienydd671972010-10-04 16:33:58 +0200378 unset($this->_cart_contents[$items['rowid']]);
Rick Ellis98783322009-02-17 02:29:44 +0000379 }
380 else
381 {
382 $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Rick Ellis98783322009-02-17 02:29:44 +0000385 return TRUE;
386 }
387
388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Rick Ellis98783322009-02-17 02:29:44 +0000390 /**
391 * Save the cart array to the session DB
392 *
393 * @access private
394 * @return bool
395 */
396 function _save_cart()
397 {
Rick Ellisaf4fb222009-02-24 22:44:22 +0000398 // Unset these so our total can be calculated correctly below
399 unset($this->_cart_contents['total_items']);
400 unset($this->_cart_contents['cart_total']);
401
402 // Lets add up the individual prices and set the cart sub-total
Rick Ellis98783322009-02-17 02:29:44 +0000403 $total = 0;
MarcosCoelho98b21262011-07-18 16:12:47 -0300404 $items = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000405 foreach ($this->_cart_contents as $key => $val)
406 {
407 // We make sure the array contains the proper indexes
Rick Ellisaf4fb222009-02-24 22:44:22 +0000408 if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000409 {
410 continue;
411 }
412
413 $total += ($val['price'] * $val['qty']);
MarcosCoelho98b21262011-07-18 16:12:47 -0300414 $items += $val['qty'];
Barry Mienydd671972010-10-04 16:33:58 +0200415
Rick Ellis98783322009-02-17 02:29:44 +0000416 // Set the subtotal
Rick Ellisdf39d512009-02-24 22:29:37 +0000417 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000418 }
419
Rick Ellis98783322009-02-17 02:29:44 +0000420 // Set the cart total and total items.
MarcosCoelho98b21262011-07-18 16:12:47 -0300421 $this->_cart_contents['total_items'] = $items;
Rick Ellis98783322009-02-17 02:29:44 +0000422 $this->_cart_contents['cart_total'] = $total;
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Jones37f4b9c2011-07-01 17:56:50 -0500424 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000425 if (count($this->_cart_contents) <= 2)
426 {
427 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200428
Rick Ellis98783322009-02-17 02:29:44 +0000429 // Nothing more to do... coffee time!
430 return FALSE;
431 }
432
433 // If we made it this far it means that our cart has data.
434 // Let's pass it to the Session class so it can be stored
435 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
436
437 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200438 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000439 }
440
441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Rick Ellis98783322009-02-17 02:29:44 +0000443 /**
444 * Cart Total
445 *
446 * @access public
447 * @return integer
448 */
449 function total()
450 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000451 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000452 }
Andrew Seymourf75ec112011-12-14 09:36:39 +0000453
454 // --------------------------------------------------------------------
455
456 /**
457 * Remove Item
458 *
459 * Removes an item from the cart
460 *
461 * @access public
462 * @return boolean
463 */
464 public function remove($rowid)
465 {
466 // just do an unset
467 unset($this->_cart_contents[$rowid]);
468
469 // we need to save the cart now we've made our changes
470 $this->_save_cart();
471
472 // completed
473 return true;
474 }
475
Rick Ellis98783322009-02-17 02:29:44 +0000476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Rick Ellis98783322009-02-17 02:29:44 +0000478 /**
479 * Total Items
480 *
481 * Returns the total item count
482 *
483 * @access public
484 * @return integer
485 */
486 function total_items()
487 {
488 return $this->_cart_contents['total_items'];
489 }
490
491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200492
Rick Ellis98783322009-02-17 02:29:44 +0000493 /**
494 * Cart Contents
495 *
496 * Returns the entire cart array
497 *
498 * @access public
499 * @return array
500 */
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000501 function contents($newest_first = false)
Rick Ellis98783322009-02-17 02:29:44 +0000502 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000503 // do we want the newest first?
504 if($newest_first)
505 {
506 // reverse the array
507 $cart = array_reverse($this->_cart_contents);
508 } else {
509 // just added first to last
510 $cart = $this->_cast_contents;
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Rick Ellis98783322009-02-17 02:29:44 +0000513 // Remove these so they don't create a problem when showing the cart table
514 unset($cart['total_items']);
515 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200516
Rick Ellis98783322009-02-17 02:29:44 +0000517 return $cart;
518 }
519
520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200521
Rick Ellis98783322009-02-17 02:29:44 +0000522 /**
523 * Has options
524 *
525 * Returns TRUE if the rowid passed to this function correlates to an item
526 * that has options associated with it.
527 *
528 * @access public
529 * @return array
530 */
531 function has_options($rowid = '')
532 {
533 if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0)
534 {
535 return FALSE;
536 }
Barry Mienydd671972010-10-04 16:33:58 +0200537
Rick Ellis98783322009-02-17 02:29:44 +0000538 return TRUE;
539 }
540
541 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200542
Rick Ellis98783322009-02-17 02:29:44 +0000543 /**
544 * Product options
545 *
546 * Returns the an array of options, for a particular product row ID
547 *
548 * @access public
549 * @return array
550 */
551 function product_options($rowid = '')
552 {
553 if ( ! isset($this->_cart_contents[$rowid]['options']))
554 {
555 return array();
556 }
557
558 return $this->_cart_contents[$rowid]['options'];
559 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000560
561 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200562
Rick Ellisdf39d512009-02-24 22:29:37 +0000563 /**
564 * Format Number
565 *
566 * Returns the supplied number with commas and a decimal point.
567 *
568 * @access public
569 * @return integer
570 */
571 function format_number($n = '')
572 {
573 if ($n == '')
574 {
575 return '';
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Rick Ellisdf39d512009-02-24 22:29:37 +0000578 // Remove anything that isn't a number or decimal point.
579 $n = trim(preg_replace('/([^0-9\.])/i', '', $n));
Barry Mienydd671972010-10-04 16:33:58 +0200580
Rick Ellisdf39d512009-02-24 22:29:37 +0000581 return number_format($n, 2, '.', ',');
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Rick Ellis98783322009-02-17 02:29:44 +0000584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200585
Rick Ellis98783322009-02-17 02:29:44 +0000586 /**
587 * Destroy the cart
588 *
589 * Empties the cart and kills the session
590 *
591 * @access public
592 * @return null
593 */
594 function destroy()
595 {
596 unset($this->_cart_contents);
Barry Mienydd671972010-10-04 16:33:58 +0200597
598 $this->_cart_contents['cart_total'] = 0;
599 $this->_cart_contents['total_items'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000600
Rick Ellis9c86ce52009-02-17 19:54:14 +0000601 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000602 }
603
604
605}
606// END Cart Class
607
608/* End of file Cart.php */
609/* Location: ./system/libraries/Cart.php */