blob: b7b0697fbd632973f580178857444b71165793a2 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2006 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Rick Ellis98783322009-02-17 02:29:44 +000028
Rick Ellis98783322009-02-17 02:29:44 +000029/**
30 * Shopping Cart Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Shopping Cart
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Rick Ellis98783322009-02-17 02:29:44 +000036 * @link http://codeigniter.com/user_guide/libraries/cart.html
37 */
38class CI_Cart {
39
Timothy Warren86611db2012-04-27 10:06:25 -040040 /**
41 * These are the regular expression rules that we use to validate the product ID and product name
42 * alpha-numeric, dashes, underscores, or periods
Andrey Andreev56454792012-05-17 14:32:19 +030043 *
Timothy Warren86611db2012-04-27 10:06:25 -040044 * @var string
45 */
46 public $product_id_rules = '\.a-z0-9_-';
Andrey Andreev56454792012-05-17 14:32:19 +030047
Timothy Warren86611db2012-04-27 10:06:25 -040048 /**
49 * These are the regular expression rules that we use to validate the product ID and product name
50 * alpha-numeric, dashes, underscores, colons or periods
51 *
52 * @var string
53 */
54 public $product_name_rules = '\.\:\-_ a-z0-9';
Andrey Andreev56454792012-05-17 14:32:19 +030055
Timothy Warren86611db2012-04-27 10:06:25 -040056 /**
57 * only allow safe product names
58 *
59 * @var bool
60 */
61 public $product_name_safe = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020062
Timothy Warren86611db2012-04-27 10:06:25 -040063 // --------------------------------------------------------------------------
Andrey Andreev6f042cc2012-03-26 14:58:33 +030064 // Protected variables. Do not change!
Timothy Warren86611db2012-04-27 10:06:25 -040065 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren86611db2012-04-27 10:06:25 -040067 /**
68 * Reference to CodeIgniter instance
69 *
70 * @var object
71 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030072 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren86611db2012-04-27 10:06:25 -040074 /**
75 * Contents of the cart
76 *
77 * @var array
78 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030079 protected $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000080
81 /**
82 * Shopping Class Constructor
83 *
84 * The constructor loads the Session class, used to store the shopping cart contents.
Timothy Warren86611db2012-04-27 10:06:25 -040085 *
86 * @param array
Andrey Andreev56454792012-05-17 14:32:19 +030087 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Greg Akera9263282010-11-10 15:26:43 -060089 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020090 {
Rick Ellis98783322009-02-17 02:29:44 +000091 // Set the super object to a local variable for use later
92 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Jones37f4b9c2011-07-01 17:56:50 -050094 // Are any config settings being passed manually? If so, set them
Andrey Andreevbb248832011-12-21 16:42:51 +020095 $config = is_array($params) ? $params : array();
Barry Mienydd671972010-10-04 16:33:58 +020096
Rick Ellis98783322009-02-17 02:29:44 +000097 // Load the Sessions class
Edwin Awedc9afa2013-01-25 16:12:20 +080098 $this->CI->load->driver('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +020099
Andrey Andreevbb248832011-12-21 16:42:51 +0200100 // Grab the shopping cart array from the session table
101 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
Ivan Tcholakov29453cd2012-11-23 13:54:28 +0200102 if ($this->_cart_contents === NULL)
Rick Ellis98783322009-02-17 02:29:44 +0000103 {
104 // No cart exists so we'll set some base values
Andrey Andreevbb248832011-12-21 16:42:51 +0200105 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis98783322009-02-17 02:29:44 +0000106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300108 log_message('debug', 'Cart Class Initialized');
Rick Ellis98783322009-02-17 02:29:44 +0000109 }
110
111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200112
Rick Ellis98783322009-02-17 02:29:44 +0000113 /**
114 * Insert items into the cart and save it to the session table
115 *
Rick Ellis98783322009-02-17 02:29:44 +0000116 * @param array
117 * @return bool
118 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200119 public function insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000120 {
121 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200122 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000123 {
124 log_message('error', 'The insert method must be passed an array containing data.');
125 return FALSE;
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
128 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +0000129 // or multiple products using a multi-dimensional one. The way we
130 // determine the array type is by looking for a required array key named "id"
131 // 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 +0200132
133 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000134 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +0200135 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600136 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000137 {
138 $save_cart = TRUE;
139 }
140 }
141 else
142 {
143 foreach ($items as $val)
144 {
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300145 if (is_array($val) && isset($val['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000146 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600147 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000148 {
149 $save_cart = TRUE;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151 }
Rick Ellis98783322009-02-17 02:29:44 +0000152 }
153 }
154
155 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200156 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000157 {
158 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600159 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000160 }
161
162 return FALSE;
163 }
164
165 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200166
Rick Ellis98783322009-02-17 02:29:44 +0000167 /**
168 * Insert
169 *
Rick Ellis98783322009-02-17 02:29:44 +0000170 * @param array
171 * @return bool
172 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300173 protected function _insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000174 {
175 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200176 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000177 {
178 log_message('error', 'The insert method must be passed an array containing data.');
179 return FALSE;
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Rick Ellis98783322009-02-17 02:29:44 +0000182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Jones37f4b9c2011-07-01 17:56:50 -0500184 // Does the $items array contain an id, quantity, price, and name? These are required
Andrey Andreev56454792012-05-17 14:32:19 +0300185 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000186 {
187 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
188 return FALSE;
189 }
190
191 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200192
Andrey Andreevbb248832011-12-21 16:42:51 +0200193 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
Andrey Andreev17779d62011-12-22 13:21:08 +0200194 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000195
196 // If the quantity is zero or blank there's nothing for us to do
Andrey Andreev1a9b7e02012-11-01 16:23:47 +0200197 if ($items['qty'] == 0)
Rick Ellis98783322009-02-17 02:29:44 +0000198 {
199 return FALSE;
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Rick Ellis98783322009-02-17 02:29:44 +0000202 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200203
Rick Ellis98783322009-02-17 02:29:44 +0000204 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
205 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
206 // Note: These can be user-specified by setting the $this->product_id_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200207 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000208 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500209 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 +0000210 return FALSE;
211 }
212
213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200214
Rick Ellis98783322009-02-17 02:29:44 +0000215 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
216 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200217 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i', $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000218 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000219 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');
220 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000221 }
222
223 // --------------------------------------------------------------------
224
Andrey Andreevbb248832011-12-21 16:42:51 +0200225 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200226 $items['price'] = (float) $items['price'];
Barry Mienydd671972010-10-04 16:33:58 +0200227
Rick Ellis98783322009-02-17 02:29:44 +0000228 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200229 // Every time something is added to the cart it is stored in the master cart array.
230 // Each row in the cart array, however, must have a unique index that identifies not only
231 // a particular product, but makes it possible to store identical products with different options.
232 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500233 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000234 // both sizes because it's the same shirt. The only difference will be the size.
235 // Internally, we need to treat identical submissions, but with different options, as a unique product.
236 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
237 // This becomes the unique "row ID"
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300238 if (isset($items['options']) && count($items['options']) > 0)
Rick Ellis98783322009-02-17 02:29:44 +0000239 {
ThallisPHPf074dff2012-05-03 16:01:46 -0300240 $rowid = md5($items['id'].serialize($items['options']));
Rick Ellis98783322009-02-17 02:29:44 +0000241 }
242 else
243 {
244 // No options were submitted so we simply MD5 the product ID.
245 // Technically, we don't need to MD5 the ID in this case, but it makes
246 // sense to standardize the format of array indexes for both conditions
247 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200248 }
Rick Ellis98783322009-02-17 02:29:44 +0000249
250 // --------------------------------------------------------------------
251
252 // 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 +0000253 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200254 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200255
Andrey Andreevbb248832011-12-21 16:42:51 +0200256 // Re-create the entry, just to make sure our index contains only the data from this submission
257 $items['rowid'] = $rowid;
258 $items['qty'] += $old_quantity;
259 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200260
Phil Sturgeon90910512011-07-20 10:07:40 -0600261 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000262 }
263
264 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200265
Rick Ellis98783322009-02-17 02:29:44 +0000266 /**
267 * Update the cart
268 *
Barry Mienydd671972010-10-04 16:33:58 +0200269 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000270 * Typically it is called from the "view cart" page if a user makes
271 * changes to the quantity before checkout. That array must contain the
272 * product ID and quantity for each item.
273 *
Rick Ellis98783322009-02-17 02:29:44 +0000274 * @param array
Rick Ellis98783322009-02-17 02:29:44 +0000275 * @return bool
276 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200277 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000278 {
279 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200280 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000281 {
282 return FALSE;
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
285 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500286 // or multiple products using a multi-dimensional one. The way we
Rick Ellis98783322009-02-17 02:29:44 +0000287 // determine the array type is by looking for a required array key named "id".
288 // If it's not found we assume it's a multi-dimensional array
289 $save_cart = FALSE;
Andrey Andreevbb248832011-12-21 16:42:51 +0200290 if (isset($items['rowid'], $items['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000291 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200292 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000293 {
294 $save_cart = TRUE;
295 }
296 }
297 else
298 {
299 foreach ($items as $val)
300 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200301 if (is_array($val) && isset($val['rowid'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000302 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200303 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000304 {
305 $save_cart = TRUE;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307 }
Rick Ellis98783322009-02-17 02:29:44 +0000308 }
309 }
310
311 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200312 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000313 {
314 $this->_save_cart();
315 return TRUE;
316 }
317
318 return FALSE;
319 }
320
321 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Rick Ellis98783322009-02-17 02:29:44 +0000323 /**
324 * Update the cart
325 *
Barry Mienydd671972010-10-04 16:33:58 +0200326 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000327 * Typically it is called from the "view cart" page if a user makes
328 * changes to the quantity before checkout. That array must contain the
329 * product ID and quantity for each item.
330 *
Rick Ellis98783322009-02-17 02:29:44 +0000331 * @param array
332 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300334 protected function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000335 {
336 // Without these array indexes there is nothing we can do
Andrey Andreev56454792012-05-17 14:32:19 +0300337 if ( ! isset($items['qty'], $items['rowid'], $this->_cart_contents[$items['rowid']]))
Rick Ellis98783322009-02-17 02:29:44 +0000338 {
339 return FALSE;
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Rick Ellis98783322009-02-17 02:29:44 +0000342 // Prep the quantity
Andrey Andreev17779d62011-12-22 13:21:08 +0200343 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000344
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 // Is the quantity zero? If so we will remove the item from the cart.
Rick Ellis98783322009-02-17 02:29:44 +0000346 // If the quantity is greater than zero we are updating
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300347 if ($items['qty'] == 0)
Rick Ellis98783322009-02-17 02:29:44 +0000348 {
Barry Mienydd671972010-10-04 16:33:58 +0200349 unset($this->_cart_contents[$items['rowid']]);
Rick Ellis98783322009-02-17 02:29:44 +0000350 }
351 else
352 {
353 $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Rick Ellis98783322009-02-17 02:29:44 +0000356 return TRUE;
357 }
358
359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Rick Ellis98783322009-02-17 02:29:44 +0000361 /**
362 * Save the cart array to the session DB
363 *
Rick Ellis98783322009-02-17 02:29:44 +0000364 * @return bool
365 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300366 protected function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000367 {
vlakoff35672462013-02-15 01:36:04 +0100368 // Let's add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200369 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000370 foreach ($this->_cart_contents as $key => $val)
371 {
372 // We make sure the array contains the proper indexes
Andrey Andreev56454792012-05-17 14:32:19 +0300373 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000374 {
375 continue;
376 }
377
Andrey Andreevbb248832011-12-21 16:42:51 +0200378 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
379 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000380 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000381 }
382
Andrey Andreev56454792012-05-17 14:32:19 +0300383 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000384 if (count($this->_cart_contents) <= 2)
385 {
386 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200387
Rick Ellis98783322009-02-17 02:29:44 +0000388 // Nothing more to do... coffee time!
389 return FALSE;
390 }
391
392 // If we made it this far it means that our cart has data.
393 // Let's pass it to the Session class so it can be stored
394 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
395
Andrey Andreev17779d62011-12-22 13:21:08 +0200396 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200397 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000398 }
399
400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Rick Ellis98783322009-02-17 02:29:44 +0000402 /**
403 * Cart Total
404 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300405 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000406 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200407 public function total()
Rick Ellis98783322009-02-17 02:29:44 +0000408 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000409 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000410 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200411
Andrew Seymourf75ec112011-12-14 09:36:39 +0000412 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200413
Andrew Seymourf75ec112011-12-14 09:36:39 +0000414 /**
415 * Remove Item
416 *
417 * Removes an item from the cart
418 *
Timothy Warren86611db2012-04-27 10:06:25 -0400419 * @param int
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300420 * @return bool
Andrew Seymourf75ec112011-12-14 09:36:39 +0000421 */
422 public function remove($rowid)
423 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200424 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000425 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000426 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200427 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000428 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200429
Rick Ellis98783322009-02-17 02:29:44 +0000430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Rick Ellis98783322009-02-17 02:29:44 +0000432 /**
433 * Total Items
434 *
435 * Returns the total item count
436 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300437 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000438 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200439 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000440 {
441 return $this->_cart_contents['total_items'];
442 }
443
444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Rick Ellis98783322009-02-17 02:29:44 +0000446 /**
447 * Cart Contents
448 *
449 * Returns the entire cart array
450 *
Timothy Warren86611db2012-04-27 10:06:25 -0400451 * @param bool
Rick Ellis98783322009-02-17 02:29:44 +0000452 * @return array
453 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200454 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000455 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000456 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200457 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200458
Rick Ellis98783322009-02-17 02:29:44 +0000459 // Remove these so they don't create a problem when showing the cart table
460 unset($cart['total_items']);
461 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200462
Rick Ellis98783322009-02-17 02:29:44 +0000463 return $cart;
464 }
465
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Rick Ellis98783322009-02-17 02:29:44 +0000468 /**
Andrey Andreevcdeee662012-10-25 17:29:52 +0300469 * Get cart item
470 *
471 * Returns the details of a specific item in the cart
472 *
473 * @param string $row_id
474 * @return array
475 */
476 public function get_item($row_id)
477 {
478 return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->_cart_contents[$row_id]))
479 ? FALSE
480 : $this->_cart_contents[$row_id];
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Rick Ellis98783322009-02-17 02:29:44 +0000486 * Has options
487 *
488 * Returns TRUE if the rowid passed to this function correlates to an item
489 * that has options associated with it.
490 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300491 * @param string $row_id = ''
Andrey Andreevbb248832011-12-21 16:42:51 +0200492 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000493 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300494 public function has_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000495 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300496 return (isset($this->_cart_contents[$row_id]['options']) && count($this->_cart_contents[$row_id]['options']) !== 0);
Rick Ellis98783322009-02-17 02:29:44 +0000497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Rick Ellis98783322009-02-17 02:29:44 +0000501 /**
502 * Product options
503 *
504 * Returns the an array of options, for a particular product row ID
505 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300506 * @param string $row_id = ''
Rick Ellis98783322009-02-17 02:29:44 +0000507 * @return array
508 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300509 public function product_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000510 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300511 return isset($this->_cart_contents[$row_id]['options']) ? $this->_cart_contents[$row_id]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000512 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000513
514 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200515
Rick Ellisdf39d512009-02-24 22:29:37 +0000516 /**
517 * Format Number
518 *
519 * Returns the supplied number with commas and a decimal point.
520 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300521 * @param float
Andrey Andreevbb248832011-12-21 16:42:51 +0200522 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000523 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200524 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000525 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100526 return ($n === '') ? '' : number_format( (float) $n, 2, '.', ',');
Rick Ellisdf39d512009-02-24 22:29:37 +0000527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Rick Ellis98783322009-02-17 02:29:44 +0000529 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200530
Rick Ellis98783322009-02-17 02:29:44 +0000531 /**
532 * Destroy the cart
533 *
534 * Empties the cart and kills the session
535 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200536 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000537 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200538 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000539 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200540 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000541 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000542 }
543
Rick Ellis98783322009-02-17 02:29:44 +0000544}
Rick Ellis98783322009-02-17 02:29:44 +0000545
546/* End of file Cart.php */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300547/* Location: ./system/libraries/Cart.php */