blob: eee1235848fe9e78c77050451b3ed55e3727047f [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
Timothy Warren86611db2012-04-27 10:06:25 -040039 /**
40 * These are the regular expression rules that we use to validate the product ID and product name
41 * alpha-numeric, dashes, underscores, or periods
42 *
43 * @var string
44 */
45 public $product_id_rules = '\.a-z0-9_-';
46
47 /**
48 * These are the regular expression rules that we use to validate the product ID and product name
49 * alpha-numeric, dashes, underscores, colons or periods
50 *
51 * @var string
52 */
53 public $product_name_rules = '\.\:\-_ a-z0-9';
54
55 /**
56 * only allow safe product names
57 *
58 * @var bool
59 */
60 public $product_name_safe = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020061
Timothy Warren86611db2012-04-27 10:06:25 -040062 // --------------------------------------------------------------------------
Andrey Andreev6f042cc2012-03-26 14:58:33 +030063 // Protected variables. Do not change!
Timothy Warren86611db2012-04-27 10:06:25 -040064 // --------------------------------------------------------------------------
65
66 /**
67 * Reference to CodeIgniter instance
68 *
69 * @var object
70 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030071 protected $CI;
Timothy Warren86611db2012-04-27 10:06:25 -040072
73 /**
74 * Contents of the cart
75 *
76 * @var array
77 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030078 protected $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000079
80 /**
81 * Shopping Class Constructor
82 *
83 * The constructor loads the Session class, used to store the shopping cart contents.
Timothy Warren86611db2012-04-27 10:06:25 -040084 *
85 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Greg Akera9263282010-11-10 15:26:43 -060087 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020088 {
Rick Ellis98783322009-02-17 02:29:44 +000089 // Set the super object to a local variable for use later
90 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Jones37f4b9c2011-07-01 17:56:50 -050092 // Are any config settings being passed manually? If so, set them
Andrey Andreevbb248832011-12-21 16:42:51 +020093 $config = is_array($params) ? $params : array();
Barry Mienydd671972010-10-04 16:33:58 +020094
Rick Ellis98783322009-02-17 02:29:44 +000095 // Load the Sessions class
96 $this->CI->load->library('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +020097
Andrey Andreevbb248832011-12-21 16:42:51 +020098 // Grab the shopping cart array from the session table
99 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
100 if ($this->_cart_contents === FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000101 {
102 // No cart exists so we'll set some base values
Andrey Andreevbb248832011-12-21 16:42:51 +0200103 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis98783322009-02-17 02:29:44 +0000104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300106 log_message('debug', 'Cart Class Initialized');
Rick Ellis98783322009-02-17 02:29:44 +0000107 }
108
109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Rick Ellis98783322009-02-17 02:29:44 +0000111 /**
112 * Insert items into the cart and save it to the session table
113 *
Rick Ellis98783322009-02-17 02:29:44 +0000114 * @param array
115 * @return bool
116 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200117 public function insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000118 {
119 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200120 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000121 {
122 log_message('error', 'The insert method must be passed an array containing data.');
123 return FALSE;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
126 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +0000127 // or multiple products using a multi-dimensional one. The way we
128 // determine the array type is by looking for a required array key named "id"
129 // 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 +0200130
131 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000132 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +0200133 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600134 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000135 {
136 $save_cart = TRUE;
137 }
138 }
139 else
140 {
141 foreach ($items as $val)
142 {
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300143 if (is_array($val) && isset($val['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000144 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600145 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000146 {
147 $save_cart = TRUE;
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149 }
Rick Ellis98783322009-02-17 02:29:44 +0000150 }
151 }
152
153 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200154 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000155 {
156 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600157 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000158 }
159
160 return FALSE;
161 }
162
163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Rick Ellis98783322009-02-17 02:29:44 +0000165 /**
166 * Insert
167 *
Rick Ellis98783322009-02-17 02:29:44 +0000168 * @param array
169 * @return bool
170 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300171 protected function _insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000172 {
173 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200174 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000175 {
176 log_message('error', 'The insert method must be passed an array containing data.');
177 return FALSE;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Rick Ellis98783322009-02-17 02:29:44 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Jones37f4b9c2011-07-01 17:56:50 -0500182 // Does the $items array contain an id, quantity, price, and name? These are required
Rick Ellis98783322009-02-17 02:29:44 +0000183 if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
184 {
185 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
186 return FALSE;
187 }
188
189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Andrey Andreevbb248832011-12-21 16:42:51 +0200191 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
Andrey Andreev17779d62011-12-22 13:21:08 +0200192 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000193
194 // If the quantity is zero or blank there's nothing for us to do
195 if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
196 {
197 return FALSE;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Rick Ellis98783322009-02-17 02:29:44 +0000200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200201
Rick Ellis98783322009-02-17 02:29:44 +0000202 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
203 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
204 // Note: These can be user-specified by setting the $this->product_id_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200205 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000206 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500207 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 +0000208 return FALSE;
209 }
210
211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Rick Ellis98783322009-02-17 02:29:44 +0000213 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
214 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200215 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i', $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000216 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000217 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');
218 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000219 }
220
221 // --------------------------------------------------------------------
222
Andrey Andreevbb248832011-12-21 16:42:51 +0200223 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200224 $items['price'] = (float) $items['price'];
Barry Mienydd671972010-10-04 16:33:58 +0200225
Rick Ellis98783322009-02-17 02:29:44 +0000226 // Is the price a valid number?
227 if ( ! is_numeric($items['price']))
228 {
229 log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
230 return FALSE;
231 }
232
233 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200234
Rick Ellis98783322009-02-17 02:29:44 +0000235 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200236 // Every time something is added to the cart it is stored in the master cart array.
237 // Each row in the cart array, however, must have a unique index that identifies not only
238 // a particular product, but makes it possible to store identical products with different options.
239 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000241 // both sizes because it's the same shirt. The only difference will be the size.
242 // Internally, we need to treat identical submissions, but with different options, as a unique product.
243 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
244 // This becomes the unique "row ID"
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300245 if (isset($items['options']) && count($items['options']) > 0)
Rick Ellis98783322009-02-17 02:29:44 +0000246 {
247 $rowid = md5($items['id'].implode('', $items['options']));
248 }
249 else
250 {
251 // No options were submitted so we simply MD5 the product ID.
252 // Technically, we don't need to MD5 the ID in this case, but it makes
253 // sense to standardize the format of array indexes for both conditions
254 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200255 }
Rick Ellis98783322009-02-17 02:29:44 +0000256
257 // --------------------------------------------------------------------
258
259 // 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 +0000260 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200261 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200262
Andrey Andreevbb248832011-12-21 16:42:51 +0200263 // Re-create the entry, just to make sure our index contains only the data from this submission
264 $items['rowid'] = $rowid;
265 $items['qty'] += $old_quantity;
266 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200267
Phil Sturgeon90910512011-07-20 10:07:40 -0600268 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000269 }
270
271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Rick Ellis98783322009-02-17 02:29:44 +0000273 /**
274 * Update the cart
275 *
Barry Mienydd671972010-10-04 16:33:58 +0200276 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000277 * Typically it is called from the "view cart" page if a user makes
278 * changes to the quantity before checkout. That array must contain the
279 * product ID and quantity for each item.
280 *
Rick Ellis98783322009-02-17 02:29:44 +0000281 * @param array
Rick Ellis98783322009-02-17 02:29:44 +0000282 * @return bool
283 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200284 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000285 {
286 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200287 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000288 {
289 return FALSE;
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
292 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500293 // or multiple products using a multi-dimensional one. The way we
Rick Ellis98783322009-02-17 02:29:44 +0000294 // determine the array type is by looking for a required array key named "id".
295 // If it's not found we assume it's a multi-dimensional array
296 $save_cart = FALSE;
Andrey Andreevbb248832011-12-21 16:42:51 +0200297 if (isset($items['rowid'], $items['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000298 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200299 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000300 {
301 $save_cart = TRUE;
302 }
303 }
304 else
305 {
306 foreach ($items as $val)
307 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200308 if (is_array($val) && isset($val['rowid'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000309 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200310 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000311 {
312 $save_cart = TRUE;
313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314 }
Rick Ellis98783322009-02-17 02:29:44 +0000315 }
316 }
317
318 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200319 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000320 {
321 $this->_save_cart();
322 return TRUE;
323 }
324
325 return FALSE;
326 }
327
328 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200329
Rick Ellis98783322009-02-17 02:29:44 +0000330 /**
331 * Update the cart
332 *
Barry Mienydd671972010-10-04 16:33:58 +0200333 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000334 * Typically it is called from the "view cart" page if a user makes
335 * changes to the quantity before checkout. That array must contain the
336 * product ID and quantity for each item.
337 *
Rick Ellis98783322009-02-17 02:29:44 +0000338 * @param array
339 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200340 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300341 protected function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000342 {
343 // Without these array indexes there is nothing we can do
344 if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']]))
345 {
346 return FALSE;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Rick Ellis98783322009-02-17 02:29:44 +0000349 // Prep the quantity
Andrey Andreev17779d62011-12-22 13:21:08 +0200350 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000351
352 // Is the quantity a number?
353 if ( ! is_numeric($items['qty']))
354 {
355 return FALSE;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Jones37f4b9c2011-07-01 17:56:50 -0500358 // Is the quantity zero? If so we will remove the item from the cart.
Rick Ellis98783322009-02-17 02:29:44 +0000359 // If the quantity is greater than zero we are updating
360 if ($items['qty'] == 0)
361 {
Barry Mienydd671972010-10-04 16:33:58 +0200362 unset($this->_cart_contents[$items['rowid']]);
Rick Ellis98783322009-02-17 02:29:44 +0000363 }
364 else
365 {
366 $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Rick Ellis98783322009-02-17 02:29:44 +0000369 return TRUE;
370 }
371
372 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200373
Rick Ellis98783322009-02-17 02:29:44 +0000374 /**
375 * Save the cart array to the session DB
376 *
Rick Ellis98783322009-02-17 02:29:44 +0000377 * @return bool
378 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300379 protected function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000380 {
Rick Ellisaf4fb222009-02-24 22:44:22 +0000381 // Lets add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200382 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000383 foreach ($this->_cart_contents as $key => $val)
384 {
385 // We make sure the array contains the proper indexes
Rick Ellisaf4fb222009-02-24 22:44:22 +0000386 if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000387 {
388 continue;
389 }
390
Andrey Andreevbb248832011-12-21 16:42:51 +0200391 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
392 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000393 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000394 }
395
Derek Jones37f4b9c2011-07-01 17:56:50 -0500396 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000397 if (count($this->_cart_contents) <= 2)
398 {
399 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200400
Rick Ellis98783322009-02-17 02:29:44 +0000401 // Nothing more to do... coffee time!
402 return FALSE;
403 }
404
405 // If we made it this far it means that our cart has data.
406 // Let's pass it to the Session class so it can be stored
407 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
408
Andrey Andreev17779d62011-12-22 13:21:08 +0200409 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200410 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000411 }
412
413 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200414
Rick Ellis98783322009-02-17 02:29:44 +0000415 /**
416 * Cart Total
417 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300418 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000419 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200420 public function total()
Rick Ellis98783322009-02-17 02:29:44 +0000421 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000422 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000423 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200424
Andrew Seymourf75ec112011-12-14 09:36:39 +0000425 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200426
Andrew Seymourf75ec112011-12-14 09:36:39 +0000427 /**
428 * Remove Item
429 *
430 * Removes an item from the cart
431 *
Timothy Warren86611db2012-04-27 10:06:25 -0400432 * @param int
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300433 * @return bool
Andrew Seymourf75ec112011-12-14 09:36:39 +0000434 */
435 public function remove($rowid)
436 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200437 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000438 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000439 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200440 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000441 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200442
Rick Ellis98783322009-02-17 02:29:44 +0000443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Rick Ellis98783322009-02-17 02:29:44 +0000445 /**
446 * Total Items
447 *
448 * Returns the total item count
449 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300450 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000451 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200452 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000453 {
454 return $this->_cart_contents['total_items'];
455 }
456
457 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200458
Rick Ellis98783322009-02-17 02:29:44 +0000459 /**
460 * Cart Contents
461 *
462 * Returns the entire cart array
463 *
Timothy Warren86611db2012-04-27 10:06:25 -0400464 * @param bool
Rick Ellis98783322009-02-17 02:29:44 +0000465 * @return array
466 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200467 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000468 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000469 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200470 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200471
Rick Ellis98783322009-02-17 02:29:44 +0000472 // Remove these so they don't create a problem when showing the cart table
473 unset($cart['total_items']);
474 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Rick Ellis98783322009-02-17 02:29:44 +0000476 return $cart;
477 }
478
479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Rick Ellis98783322009-02-17 02:29:44 +0000481 /**
482 * Has options
483 *
484 * Returns TRUE if the rowid passed to this function correlates to an item
485 * that has options associated with it.
486 *
Timothy Warren86611db2012-04-27 10:06:25 -0400487 * @param mixed
Andrey Andreevbb248832011-12-21 16:42:51 +0200488 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000489 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200490 public function has_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000491 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200492 return (isset($this->_cart_contents[$rowid]['options']) && count($this->_cart_contents[$rowid]['options']) !== 0) ? TRUE : FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000493 }
494
495 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200496
Rick Ellis98783322009-02-17 02:29:44 +0000497 /**
498 * Product options
499 *
500 * Returns the an array of options, for a particular product row ID
501 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300502 * @param int
Rick Ellis98783322009-02-17 02:29:44 +0000503 * @return array
504 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200505 public function product_options($rowid = '')
Rick Ellis98783322009-02-17 02:29:44 +0000506 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200507 return isset($this->_cart_contents[$rowid]['options']) ? $this->_cart_contents[$rowid]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000508 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000509
510 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200511
Rick Ellisdf39d512009-02-24 22:29:37 +0000512 /**
513 * Format Number
514 *
515 * Returns the supplied number with commas and a decimal point.
516 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300517 * @param float
Andrey Andreevbb248832011-12-21 16:42:51 +0200518 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000519 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200520 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000521 {
522 if ($n == '')
523 {
524 return '';
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Rick Ellisdf39d512009-02-24 22:29:37 +0000527 // Remove anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200528 $n = (float) $n;
Barry Mienydd671972010-10-04 16:33:58 +0200529
Rick Ellisdf39d512009-02-24 22:29:37 +0000530 return number_format($n, 2, '.', ',');
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Rick Ellis98783322009-02-17 02:29:44 +0000533 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200534
Rick Ellis98783322009-02-17 02:29:44 +0000535 /**
536 * Destroy the cart
537 *
538 * Empties the cart and kills the session
539 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200540 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000541 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200542 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000543 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200544 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000545 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000546 }
547
Rick Ellis98783322009-02-17 02:29:44 +0000548}
Rick Ellis98783322009-02-17 02:29:44 +0000549
550/* End of file Cart.php */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300551/* Location: ./system/libraries/Cart.php */