blob: 305960f5fd36744aaf6c3664d21c46d1a3827728 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Rick Ellis98783322009-02-17 02:29:44 +000028/**
29 * Shopping Cart Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Shopping Cart
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Rick Ellis98783322009-02-17 02:29:44 +000035 * @link http://codeigniter.com/user_guide/libraries/cart.html
36 */
37class CI_Cart {
38
39 // These are the regular expression rules that we use to validate the product ID and product name
Andrey Andreevbb248832011-12-21 16:42:51 +020040 public $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods
41 public $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods
Andrey Andreev6f042cc2012-03-26 14:58:33 +030042 public $product_name_safe = TRUE; // only allow safe product names
Barry Mienydd671972010-10-04 16:33:58 +020043
Andrey Andreev6f042cc2012-03-26 14:58:33 +030044 // Protected variables. Do not change!
45 protected $CI;
46 protected $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000047
48 /**
49 * Shopping Class Constructor
50 *
51 * The constructor loads the Session class, used to store the shopping cart contents.
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Greg Akera9263282010-11-10 15:26:43 -060053 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020054 {
Rick Ellis98783322009-02-17 02:29:44 +000055 // Set the super object to a local variable for use later
56 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Jones37f4b9c2011-07-01 17:56:50 -050058 // Are any config settings being passed manually? If so, set them
Andrey Andreevbb248832011-12-21 16:42:51 +020059 $config = is_array($params) ? $params : array();
Barry Mienydd671972010-10-04 16:33:58 +020060
Rick Ellis98783322009-02-17 02:29:44 +000061 // Load the Sessions class
62 $this->CI->load->library('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +020063
Andrey Andreevbb248832011-12-21 16:42:51 +020064 // Grab the shopping cart array from the session table
65 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
66 if ($this->_cart_contents === FALSE)
Rick Ellis98783322009-02-17 02:29:44 +000067 {
68 // No cart exists so we'll set some base values
Andrey Andreevbb248832011-12-21 16:42:51 +020069 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis98783322009-02-17 02:29:44 +000070 }
Barry Mienydd671972010-10-04 16:33:58 +020071
Andrey Andreev6f042cc2012-03-26 14:58:33 +030072 log_message('debug', 'Cart Class Initialized');
Rick Ellis98783322009-02-17 02:29:44 +000073 }
74
75 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020076
Rick Ellis98783322009-02-17 02:29:44 +000077 /**
78 * Insert items into the cart and save it to the session table
79 *
Rick Ellis98783322009-02-17 02:29:44 +000080 * @param array
81 * @return bool
82 */
Andrey Andreevbb248832011-12-21 16:42:51 +020083 public function insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +000084 {
85 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +020086 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +000087 {
88 log_message('error', 'The insert method must be passed an array containing data.');
89 return FALSE;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
92 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +000093 // or multiple products using a multi-dimensional one. The way we
94 // determine the array type is by looking for a required array key named "id"
95 // 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 +020096
97 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +000098 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +020099 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600100 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000101 {
102 $save_cart = TRUE;
103 }
104 }
105 else
106 {
107 foreach ($items as $val)
108 {
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300109 if (is_array($val) && isset($val['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000110 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600111 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000112 {
113 $save_cart = TRUE;
114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115 }
Rick Ellis98783322009-02-17 02:29:44 +0000116 }
117 }
118
119 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200120 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000121 {
122 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600123 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000124 }
125
126 return FALSE;
127 }
128
129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200130
Rick Ellis98783322009-02-17 02:29:44 +0000131 /**
132 * Insert
133 *
Rick Ellis98783322009-02-17 02:29:44 +0000134 * @param array
135 * @return bool
136 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200137 private function _insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000138 {
139 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200140 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000141 {
142 log_message('error', 'The insert method must be passed an array containing data.');
143 return FALSE;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Rick Ellis98783322009-02-17 02:29:44 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Jones37f4b9c2011-07-01 17:56:50 -0500148 // Does the $items array contain an id, quantity, price, and name? These are required
Rick Ellis98783322009-02-17 02:29:44 +0000149 if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
150 {
151 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
152 return FALSE;
153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Andrey Andreevbb248832011-12-21 16:42:51 +0200157 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
Andrey Andreev17779d62011-12-22 13:21:08 +0200158 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000159
160 // If the quantity is zero or blank there's nothing for us to do
161 if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
162 {
163 return FALSE;
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Rick Ellis98783322009-02-17 02:29:44 +0000166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Rick Ellis98783322009-02-17 02:29:44 +0000168 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
169 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
170 // Note: These can be user-specified by setting the $this->product_id_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200171 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000172 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500173 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 +0000174 return FALSE;
175 }
176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Rick Ellis98783322009-02-17 02:29:44 +0000179 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
180 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200181 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i', $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000182 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000183 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');
184 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000185 }
186
187 // --------------------------------------------------------------------
188
Andrey Andreevbb248832011-12-21 16:42:51 +0200189 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200190 $items['price'] = (float) $items['price'];
Barry Mienydd671972010-10-04 16:33:58 +0200191
Rick Ellis98783322009-02-17 02:29:44 +0000192 // Is the price a valid number?
193 if ( ! is_numeric($items['price']))
194 {
195 log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
196 return FALSE;
197 }
198
199 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Rick Ellis98783322009-02-17 02:29:44 +0000201 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200202 // Every time something is added to the cart it is stored in the master cart array.
203 // Each row in the cart array, however, must have a unique index that identifies not only
204 // a particular product, but makes it possible to store identical products with different options.
205 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500206 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000207 // both sizes because it's the same shirt. The only difference will be the size.
208 // Internally, we need to treat identical submissions, but with different options, as a unique product.
209 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
210 // This becomes the unique "row ID"
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300211 if (isset($items['options']) && count($items['options']) > 0)
Rick Ellis98783322009-02-17 02:29:44 +0000212 {
213 $rowid = md5($items['id'].implode('', $items['options']));
214 }
215 else
216 {
217 // No options were submitted so we simply MD5 the product ID.
218 // Technically, we don't need to MD5 the ID in this case, but it makes
219 // sense to standardize the format of array indexes for both conditions
220 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200221 }
Rick Ellis98783322009-02-17 02:29:44 +0000222
223 // --------------------------------------------------------------------
224
225 // 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 +0000226 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200227 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreevbb248832011-12-21 16:42:51 +0200229 // Re-create the entry, just to make sure our index contains only the data from this submission
230 $items['rowid'] = $rowid;
231 $items['qty'] += $old_quantity;
232 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200233
Phil Sturgeon90910512011-07-20 10:07:40 -0600234 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000235 }
236
237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200238
Rick Ellis98783322009-02-17 02:29:44 +0000239 /**
240 * Update the cart
241 *
Barry Mienydd671972010-10-04 16:33:58 +0200242 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000243 * Typically it is called from the "view cart" page if a user makes
244 * changes to the quantity before checkout. That array must contain the
245 * product ID and quantity for each item.
246 *
Rick Ellis98783322009-02-17 02:29:44 +0000247 * @param array
248 * @param string
249 * @return bool
250 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200251 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000252 {
253 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200254 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000255 {
256 return FALSE;
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
259 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500260 // or multiple products using a multi-dimensional one. The way we
Rick Ellis98783322009-02-17 02:29:44 +0000261 // determine the array type is by looking for a required array key named "id".
262 // If it's not found we assume it's a multi-dimensional array
263 $save_cart = FALSE;
Andrey Andreevbb248832011-12-21 16:42:51 +0200264 if (isset($items['rowid'], $items['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000265 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200266 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000267 {
268 $save_cart = TRUE;
269 }
270 }
271 else
272 {
273 foreach ($items as $val)
274 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200275 if (is_array($val) && isset($val['rowid'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000276 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200277 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000278 {
279 $save_cart = TRUE;
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281 }
Rick Ellis98783322009-02-17 02:29:44 +0000282 }
283 }
284
285 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200286 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000287 {
288 $this->_save_cart();
289 return TRUE;
290 }
291
292 return FALSE;
293 }
294
295 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200296
Rick Ellis98783322009-02-17 02:29:44 +0000297 /**
298 * Update the cart
299 *
Barry Mienydd671972010-10-04 16:33:58 +0200300 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000301 * Typically it is called from the "view cart" page if a user makes
302 * changes to the quantity before checkout. That array must contain the
303 * product ID and quantity for each item.
304 *
Rick Ellis98783322009-02-17 02:29:44 +0000305 * @param array
306 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200307 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300308 protected function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000309 {
310 // Without these array indexes there is nothing we can do
311 if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))
312 {
313 return FALSE;
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Rick Ellis98783322009-02-17 02:29:44 +0000316 // Prep the quantity
Andrey Andreev17779d62011-12-22 13:21:08 +0200317 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000318
319 // Is the quantity a number?
320 if ( ! is_numeric($items['qty']))
321 {
322 return FALSE;
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Jones37f4b9c2011-07-01 17:56:50 -0500325 // Is the quantity zero? If so we will remove the item from the cart.
Rick Ellis98783322009-02-17 02:29:44 +0000326 // If the quantity is greater than zero we are updating
327 if ($items['qty'] == 0)
328 {
Barry Mienydd671972010-10-04 16:33:58 +0200329 unset($this->_cart_contents[$items['rowid']]);
Rick Ellis98783322009-02-17 02:29:44 +0000330 }
331 else
332 {
333 $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Rick Ellis98783322009-02-17 02:29:44 +0000336 return TRUE;
337 }
338
339 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200340
Rick Ellis98783322009-02-17 02:29:44 +0000341 /**
342 * Save the cart array to the session DB
343 *
Rick Ellis98783322009-02-17 02:29:44 +0000344 * @return bool
345 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300346 protected function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000347 {
Rick Ellisaf4fb222009-02-24 22:44:22 +0000348 // Lets add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200349 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000350 foreach ($this->_cart_contents as $key => $val)
351 {
352 // We make sure the array contains the proper indexes
Rick Ellisaf4fb222009-02-24 22:44:22 +0000353 if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000354 {
355 continue;
356 }
357
Andrey Andreevbb248832011-12-21 16:42:51 +0200358 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
359 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000360 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000361 }
362
Derek Jones37f4b9c2011-07-01 17:56:50 -0500363 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000364 if (count($this->_cart_contents) <= 2)
365 {
366 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200367
Rick Ellis98783322009-02-17 02:29:44 +0000368 // Nothing more to do... coffee time!
369 return FALSE;
370 }
371
372 // If we made it this far it means that our cart has data.
373 // Let's pass it to the Session class so it can be stored
374 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
375
Andrey Andreev17779d62011-12-22 13:21:08 +0200376 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200377 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000378 }
379
380 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200381
Rick Ellis98783322009-02-17 02:29:44 +0000382 /**
383 * Cart Total
384 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300385 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000386 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200387 public function total()
Rick Ellis98783322009-02-17 02:29:44 +0000388 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000389 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000390 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200391
Andrew Seymourf75ec112011-12-14 09:36:39 +0000392 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200393
Andrew Seymourf75ec112011-12-14 09:36:39 +0000394 /**
395 * Remove Item
396 *
397 * Removes an item from the cart
398 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300399 * @return bool
Andrew Seymourf75ec112011-12-14 09:36:39 +0000400 */
401 public function remove($rowid)
402 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200403 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000404 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000405 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200406 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000407 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200408
Rick Ellis98783322009-02-17 02:29:44 +0000409 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200410
Rick Ellis98783322009-02-17 02:29:44 +0000411 /**
412 * Total Items
413 *
414 * Returns the total item count
415 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300416 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000417 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200418 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000419 {
420 return $this->_cart_contents['total_items'];
421 }
422
423 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200424
Rick Ellis98783322009-02-17 02:29:44 +0000425 /**
426 * Cart Contents
427 *
428 * Returns the entire cart array
429 *
Rick Ellis98783322009-02-17 02:29:44 +0000430 * @return array
431 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200432 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000433 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000434 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200435 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200436
Rick Ellis98783322009-02-17 02:29:44 +0000437 // Remove these so they don't create a problem when showing the cart table
438 unset($cart['total_items']);
439 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200440
Rick Ellis98783322009-02-17 02:29:44 +0000441 return $cart;
442 }
443
444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Rick Ellis98783322009-02-17 02:29:44 +0000446 /**
447 * Has options
448 *
449 * Returns TRUE if the rowid passed to this function correlates to an item
450 * that has options associated with it.
451 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200452 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000453 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200454 public function has_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000455 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200456 return (isset($this->_cart_contents[$rowid]['options']) && count($this->_cart_contents[$rowid]['options']) !== 0) ? TRUE : FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000457 }
458
459 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200460
Rick Ellis98783322009-02-17 02:29:44 +0000461 /**
462 * Product options
463 *
464 * Returns the an array of options, for a particular product row ID
465 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300466 * @param int
Rick Ellis98783322009-02-17 02:29:44 +0000467 * @return array
468 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200469 public function product_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000470 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200471 return isset($this->_cart_contents[$rowid]['options']) ? $this->_cart_contents[$rowid]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000472 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Rick Ellisdf39d512009-02-24 22:29:37 +0000476 /**
477 * Format Number
478 *
479 * Returns the supplied number with commas and a decimal point.
480 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300481 * @param float
Andrey Andreevbb248832011-12-21 16:42:51 +0200482 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000483 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200484 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000485 {
486 if ($n == '')
487 {
488 return '';
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Rick Ellisdf39d512009-02-24 22:29:37 +0000491 // Remove anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200492 $n = (float) $n;
Barry Mienydd671972010-10-04 16:33:58 +0200493
Rick Ellisdf39d512009-02-24 22:29:37 +0000494 return number_format($n, 2, '.', ',');
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Rick Ellis98783322009-02-17 02:29:44 +0000497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Rick Ellis98783322009-02-17 02:29:44 +0000499 /**
500 * Destroy the cart
501 *
502 * Empties the cart and kills the session
503 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200504 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000505 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200506 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000507 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200508 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000509 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000510 }
511
Rick Ellis98783322009-02-17 02:29:44 +0000512}
Rick Ellis98783322009-02-17 02:29:44 +0000513
514/* End of file Cart.php */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300515/* Location: ./system/libraries/Cart.php */