blob: b2cc2081efaa5b94631f527c70d43b3b9ea2c69d [file] [log] [blame]
Andrey Andreevbb248832011-12-21 16:42:51 +02001<?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
Andrey Andreevbb248832011-12-21 16:42:51 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevbb248832011-12-21 16:42:51 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreevbb248832011-12-21 16:42:51 +020042 public $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
43 public $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
44 public $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!
Andrey Andreevbb248832011-12-21 16:42:51 +020047 private $CI;
48 private $_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
Andrey Andreevbb248832011-12-21 16:42:51 +020062 $config = is_array($params) ? $params : array();
Barry Mienydd671972010-10-04 16:33:58 +020063
Rick Ellis98783322009-02-17 02:29:44 +000064 // Load the Sessions class
65 $this->CI->load->library('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +020066
Andrey Andreevbb248832011-12-21 16:42:51 +020067 // Grab the shopping cart array from the session table
68 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
69 if ($this->_cart_contents === FALSE)
Rick Ellis98783322009-02-17 02:29:44 +000070 {
71 // No cart exists so we'll set some base values
Andrey Andreevbb248832011-12-21 16:42:51 +020072 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis98783322009-02-17 02:29:44 +000073 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Rick Ellis98783322009-02-17 02:29:44 +000075 log_message('debug', "Cart Class Initialized");
76 }
77
78 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020079
Rick Ellis98783322009-02-17 02:29:44 +000080 /**
81 * Insert items into the cart and save it to the session table
82 *
83 * @access public
84 * @param array
85 * @return bool
86 */
Andrey Andreevbb248832011-12-21 16:42:51 +020087 public function insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +000088 {
89 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +020090 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +000091 {
92 log_message('error', 'The insert method must be passed an array containing data.');
93 return FALSE;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
96 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +000097 // or multiple products using a multi-dimensional one. The way we
98 // determine the array type is by looking for a required array key named "id"
99 // 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 +0200100
101 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000102 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +0200103 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600104 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000105 {
106 $save_cart = TRUE;
107 }
108 }
109 else
110 {
111 foreach ($items as $val)
112 {
113 if (is_array($val) AND isset($val['id']))
114 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600115 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000116 {
117 $save_cart = TRUE;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119 }
Rick Ellis98783322009-02-17 02:29:44 +0000120 }
121 }
122
123 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200124 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000125 {
126 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600127 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000128 }
129
130 return FALSE;
131 }
132
133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Rick Ellis98783322009-02-17 02:29:44 +0000135 /**
136 * Insert
137 *
138 * @access private
139 * @param array
140 * @return bool
141 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200142 private function _insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000143 {
144 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200145 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000146 {
147 log_message('error', 'The insert method must be passed an array containing data.');
148 return FALSE;
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Rick Ellis98783322009-02-17 02:29:44 +0000151 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Jones37f4b9c2011-07-01 17:56:50 -0500153 // Does the $items array contain an id, quantity, price, and name? These are required
Rick Ellis98783322009-02-17 02:29:44 +0000154 if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
155 {
156 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
157 return FALSE;
158 }
159
160 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200161
Andrey Andreevbb248832011-12-21 16:42:51 +0200162 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
163 $items['qty'] = ltrim(trim(preg_replace('/([^0-9])/i', '', $items['qty'])), '0');
Rick Ellis98783322009-02-17 02:29:44 +0000164
165 // If the quantity is zero or blank there's nothing for us to do
166 if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
167 {
168 return FALSE;
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Rick Ellis98783322009-02-17 02:29:44 +0000171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Rick Ellis98783322009-02-17 02:29:44 +0000173 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
174 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
175 // Note: These can be user-specified by setting the $this->product_id_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200176 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000177 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500178 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 +0000179 return FALSE;
180 }
181
182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Rick Ellis98783322009-02-17 02:29:44 +0000184 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
185 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200186 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i', $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000187 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000188 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');
189 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000190 }
191
192 // --------------------------------------------------------------------
193
Andrey Andreevbb248832011-12-21 16:42:51 +0200194 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
195 $items['price'] = lrtrim(trim(preg_replace('/([^0-9\.])/i', '', $items['price'])), '0');
Barry Mienydd671972010-10-04 16:33:58 +0200196
Rick Ellis98783322009-02-17 02:29:44 +0000197 // Is the price a valid number?
198 if ( ! is_numeric($items['price']))
199 {
200 log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
201 return FALSE;
202 }
203
204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Rick Ellis98783322009-02-17 02:29:44 +0000206 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200207 // Every time something is added to the cart it is stored in the master cart array.
208 // Each row in the cart array, however, must have a unique index that identifies not only
209 // a particular product, but makes it possible to store identical products with different options.
210 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500211 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000212 // both sizes because it's the same shirt. The only difference will be the size.
213 // Internally, we need to treat identical submissions, but with different options, as a unique product.
214 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
215 // This becomes the unique "row ID"
216 if (isset($items['options']) AND count($items['options']) > 0)
217 {
218 $rowid = md5($items['id'].implode('', $items['options']));
219 }
220 else
221 {
222 // No options were submitted so we simply MD5 the product ID.
223 // Technically, we don't need to MD5 the ID in this case, but it makes
224 // sense to standardize the format of array indexes for both conditions
225 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
Rick Ellis98783322009-02-17 02:29:44 +0000227
228 // --------------------------------------------------------------------
229
230 // 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 +0000231 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200232 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200233
Andrey Andreevbb248832011-12-21 16:42:51 +0200234 // Re-create the entry, just to make sure our index contains only the data from this submission
235 $items['rowid'] = $rowid;
236 $items['qty'] += $old_quantity;
237 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200238
Phil Sturgeon90910512011-07-20 10:07:40 -0600239 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000240 }
241
242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200243
Rick Ellis98783322009-02-17 02:29:44 +0000244 /**
245 * Update the cart
246 *
Barry Mienydd671972010-10-04 16:33:58 +0200247 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000248 * Typically it is called from the "view cart" page if a user makes
249 * changes to the quantity before checkout. That array must contain the
250 * product ID and quantity for each item.
251 *
252 * @access public
253 * @param array
254 * @param string
255 * @return bool
256 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200257 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000258 {
259 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200260 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000261 {
262 return FALSE;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
265 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500266 // or multiple products using a multi-dimensional one. The way we
Rick Ellis98783322009-02-17 02:29:44 +0000267 // determine the array type is by looking for a required array key named "id".
268 // If it's not found we assume it's a multi-dimensional array
269 $save_cart = FALSE;
Andrey Andreevbb248832011-12-21 16:42:51 +0200270 if (isset($items['rowid'], $items['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000271 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200272 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000273 {
274 $save_cart = TRUE;
275 }
276 }
277 else
278 {
279 foreach ($items as $val)
280 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200281 if (is_array($val) && isset($val['rowid'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000282 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200283 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000284 {
285 $save_cart = TRUE;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287 }
Rick Ellis98783322009-02-17 02:29:44 +0000288 }
289 }
290
291 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200292 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000293 {
294 $this->_save_cart();
295 return TRUE;
296 }
297
298 return FALSE;
299 }
300
301 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200302
Rick Ellis98783322009-02-17 02:29:44 +0000303 /**
304 * Update the cart
305 *
Barry Mienydd671972010-10-04 16:33:58 +0200306 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000307 * Typically it is called from the "view cart" page if a user makes
308 * changes to the quantity before checkout. That array must contain the
309 * product ID and quantity for each item.
310 *
311 * @access private
312 * @param array
313 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200315 private function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000316 {
317 // Without these array indexes there is nothing we can do
318 if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))
319 {
320 return FALSE;
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Rick Ellis98783322009-02-17 02:29:44 +0000323 // Prep the quantity
324 $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
325
326 // Is the quantity a number?
327 if ( ! is_numeric($items['qty']))
328 {
329 return FALSE;
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Rick Ellis98783322009-02-17 02:29:44 +0000332 // Is the new quantity different than what is already saved in the cart?
333 // If it's the same there's nothing to do
334 if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])
335 {
336 return FALSE;
337 }
338
Derek Jones37f4b9c2011-07-01 17:56:50 -0500339 // Is the quantity zero? If so we will remove the item from the cart.
Rick Ellis98783322009-02-17 02:29:44 +0000340 // If the quantity is greater than zero we are updating
341 if ($items['qty'] == 0)
342 {
Barry Mienydd671972010-10-04 16:33:58 +0200343 unset($this->_cart_contents[$items['rowid']]);
Rick Ellis98783322009-02-17 02:29:44 +0000344 }
345 else
346 {
347 $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Rick Ellis98783322009-02-17 02:29:44 +0000350 return TRUE;
351 }
352
353 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200354
Rick Ellis98783322009-02-17 02:29:44 +0000355 /**
356 * Save the cart array to the session DB
357 *
358 * @access private
359 * @return bool
360 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200361 private function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000362 {
Rick Ellisaf4fb222009-02-24 22:44:22 +0000363 // Lets add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200364 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000365 foreach ($this->_cart_contents as $key => $val)
366 {
367 // We make sure the array contains the proper indexes
Rick Ellisaf4fb222009-02-24 22:44:22 +0000368 if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000369 {
370 continue;
371 }
372
Andrey Andreevbb248832011-12-21 16:42:51 +0200373 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
374 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000375 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000376 }
377
Derek Jones37f4b9c2011-07-01 17:56:50 -0500378 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000379 if (count($this->_cart_contents) <= 2)
380 {
381 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200382
Rick Ellis98783322009-02-17 02:29:44 +0000383 // Nothing more to do... coffee time!
384 return FALSE;
385 }
386
387 // If we made it this far it means that our cart has data.
388 // Let's pass it to the Session class so it can be stored
389 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
390
Barry Mienydd671972010-10-04 16:33:58 +0200391 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Rick Ellis98783322009-02-17 02:29:44 +0000396 /**
397 * Cart Total
398 *
399 * @access public
400 * @return integer
401 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200402 public function total()
Rick Ellis98783322009-02-17 02:29:44 +0000403 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000404 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000405 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200406
Andrew Seymourf75ec112011-12-14 09:36:39 +0000407 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200408
Andrew Seymourf75ec112011-12-14 09:36:39 +0000409 /**
410 * Remove Item
411 *
412 * Removes an item from the cart
413 *
414 * @access public
415 * @return boolean
416 */
417 public function remove($rowid)
418 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200419 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000420 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000421 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200422 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000423 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200424
Rick Ellis98783322009-02-17 02:29:44 +0000425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Rick Ellis98783322009-02-17 02:29:44 +0000427 /**
428 * Total Items
429 *
430 * Returns the total item count
431 *
432 * @access public
433 * @return integer
434 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200435 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000436 {
437 return $this->_cart_contents['total_items'];
438 }
439
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Rick Ellis98783322009-02-17 02:29:44 +0000442 /**
443 * Cart Contents
444 *
445 * Returns the entire cart array
446 *
447 * @access public
448 * @return array
449 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200450 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000451 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000452 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200453 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200454
Rick Ellis98783322009-02-17 02:29:44 +0000455 // Remove these so they don't create a problem when showing the cart table
456 unset($cart['total_items']);
457 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200458
Rick Ellis98783322009-02-17 02:29:44 +0000459 return $cart;
460 }
461
462 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200463
Rick Ellis98783322009-02-17 02:29:44 +0000464 /**
465 * Has options
466 *
467 * Returns TRUE if the rowid passed to this function correlates to an item
468 * that has options associated with it.
469 *
470 * @access public
Andrey Andreevbb248832011-12-21 16:42:51 +0200471 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000472 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200473 public function has_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000474 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200475 return (isset($this->_cart_contents[$rowid]['options']) && count($this->_cart_contents[$rowid]['options']) !== 0) ? TRUE : FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000476 }
477
478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Rick Ellis98783322009-02-17 02:29:44 +0000480 /**
481 * Product options
482 *
483 * Returns the an array of options, for a particular product row ID
484 *
485 * @access public
486 * @return array
487 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200488 public function product_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000489 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200490 return isset($this->_cart_contents[$rowid]['options']) ? $this->_cart_contents[$rowid]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000491 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000492
493 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200494
Rick Ellisdf39d512009-02-24 22:29:37 +0000495 /**
496 * Format Number
497 *
498 * Returns the supplied number with commas and a decimal point.
499 *
500 * @access public
Andrey Andreevbb248832011-12-21 16:42:51 +0200501 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000502 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200503 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000504 {
505 if ($n == '')
506 {
507 return '';
508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Rick Ellisdf39d512009-02-24 22:29:37 +0000510 // Remove anything that isn't a number or decimal point.
511 $n = trim(preg_replace('/([^0-9\.])/i', '', $n));
Barry Mienydd671972010-10-04 16:33:58 +0200512
Rick Ellisdf39d512009-02-24 22:29:37 +0000513 return number_format($n, 2, '.', ',');
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Rick Ellis98783322009-02-17 02:29:44 +0000516 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200517
Rick Ellis98783322009-02-17 02:29:44 +0000518 /**
519 * Destroy the cart
520 *
521 * Empties the cart and kills the session
522 *
523 * @access public
Andrey Andreevbb248832011-12-21 16:42:51 +0200524 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000525 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200526 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000527 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200528 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000529 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000530 }
531
532
533}
534// END Cart Class
535
536/* End of file Cart.php */
Andrey Andreevbb248832011-12-21 16:42:51 +0200537/* Location: ./system/libraries/Cart.php */