blob: 14f08a8c3e3efbcc46d5157742d32b9791f123ba [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevbb248832011-12-21 16:42:51 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevbb248832011-12-21 16:42:51 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
31 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
32 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Rick Ellis98783322009-02-17 02:29:44 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Rick Ellis98783322009-02-17 02:29:44 +000039
Rick Ellis98783322009-02-17 02:29:44 +000040/**
41 * Shopping Cart Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Shopping Cart
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Rick Ellis98783322009-02-17 02:29:44 +000047 * @link http://codeigniter.com/user_guide/libraries/cart.html
48 */
49class CI_Cart {
50
Timothy Warren86611db2012-04-27 10:06:25 -040051 /**
52 * These are the regular expression rules that we use to validate the product ID and product name
53 * alpha-numeric, dashes, underscores, or periods
Andrey Andreev56454792012-05-17 14:32:19 +030054 *
Timothy Warren86611db2012-04-27 10:06:25 -040055 * @var string
56 */
57 public $product_id_rules = '\.a-z0-9_-';
Andrey Andreev56454792012-05-17 14:32:19 +030058
Timothy Warren86611db2012-04-27 10:06:25 -040059 /**
60 * These are the regular expression rules that we use to validate the product ID and product name
61 * alpha-numeric, dashes, underscores, colons or periods
62 *
63 * @var string
64 */
Louis Racicot65b8f832013-03-11 09:03:25 -040065 public $product_name_rules = '\w \-\.\:';
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren86611db2012-04-27 10:06:25 -040067 /**
68 * only allow safe product names
69 *
70 * @var bool
71 */
72 public $product_name_safe = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020073
Timothy Warren86611db2012-04-27 10:06:25 -040074 // --------------------------------------------------------------------------
Andrey Andreev56454792012-05-17 14:32:19 +030075
Timothy Warren86611db2012-04-27 10:06:25 -040076 /**
77 * Reference to CodeIgniter instance
78 *
79 * @var object
80 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030081 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030082
Timothy Warren86611db2012-04-27 10:06:25 -040083 /**
84 * Contents of the cart
85 *
86 * @var array
87 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030088 protected $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000089
90 /**
91 * Shopping Class Constructor
92 *
93 * The constructor loads the Session class, used to store the shopping cart contents.
Timothy Warren86611db2012-04-27 10:06:25 -040094 *
95 * @param array
Andrey Andreev56454792012-05-17 14:32:19 +030096 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Greg Akera9263282010-11-10 15:26:43 -060098 public function __construct($params = array())
Barry Mienydd671972010-10-04 16:33:58 +020099 {
Rick Ellis98783322009-02-17 02:29:44 +0000100 // Set the super object to a local variable for use later
101 $this->CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jones37f4b9c2011-07-01 17:56:50 -0500103 // Are any config settings being passed manually? If so, set them
Andrey Andreevbb248832011-12-21 16:42:51 +0200104 $config = is_array($params) ? $params : array();
Barry Mienydd671972010-10-04 16:33:58 +0200105
Rick Ellis98783322009-02-17 02:29:44 +0000106 // Load the Sessions class
Edwin Awedc9afa2013-01-25 16:12:20 +0800107 $this->CI->load->driver('session', $config);
Barry Mienydd671972010-10-04 16:33:58 +0200108
Andrey Andreevbb248832011-12-21 16:42:51 +0200109 // Grab the shopping cart array from the session table
110 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
Ivan Tcholakov29453cd2012-11-23 13:54:28 +0200111 if ($this->_cart_contents === NULL)
Rick Ellis98783322009-02-17 02:29:44 +0000112 {
113 // No cart exists so we'll set some base values
Andrey Andreevbb248832011-12-21 16:42:51 +0200114 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis98783322009-02-17 02:29:44 +0000115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300117 log_message('debug', 'Cart Class Initialized');
Rick Ellis98783322009-02-17 02:29:44 +0000118 }
119
120 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121
Rick Ellis98783322009-02-17 02:29:44 +0000122 /**
123 * Insert items into the cart and save it to the session table
124 *
Rick Ellis98783322009-02-17 02:29:44 +0000125 * @param array
126 * @return bool
127 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200128 public function insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000129 {
130 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200131 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000132 {
133 log_message('error', 'The insert method must be passed an array containing data.');
134 return FALSE;
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
137 // You can either insert a single product using a one-dimensional array,
Rick Ellis98783322009-02-17 02:29:44 +0000138 // or multiple products using a multi-dimensional one. The way we
139 // determine the array type is by looking for a required array key named "id"
140 // 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 +0200141
142 $save_cart = FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000143 if (isset($items['id']))
Barry Mienydd671972010-10-04 16:33:58 +0200144 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600145 if (($rowid = $this->_insert($items)))
Rick Ellis98783322009-02-17 02:29:44 +0000146 {
147 $save_cart = TRUE;
148 }
149 }
150 else
151 {
152 foreach ($items as $val)
153 {
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300154 if (is_array($val) && isset($val['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000155 {
Phil Sturgeon90910512011-07-20 10:07:40 -0600156 if ($this->_insert($val))
Rick Ellis98783322009-02-17 02:29:44 +0000157 {
158 $save_cart = TRUE;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160 }
Rick Ellis98783322009-02-17 02:29:44 +0000161 }
162 }
163
164 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200165 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000166 {
167 $this->_save_cart();
Phil Sturgeon90910512011-07-20 10:07:40 -0600168 return isset($rowid) ? $rowid : TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000169 }
170
171 return FALSE;
172 }
173
174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Rick Ellis98783322009-02-17 02:29:44 +0000176 /**
177 * Insert
178 *
Rick Ellis98783322009-02-17 02:29:44 +0000179 * @param array
180 * @return bool
181 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300182 protected function _insert($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000183 {
184 // Was any cart data passed? No? Bah...
Andrey Andreevbb248832011-12-21 16:42:51 +0200185 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000186 {
187 log_message('error', 'The insert method must be passed an array containing data.');
188 return FALSE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Rick Ellis98783322009-02-17 02:29:44 +0000191 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Jones37f4b9c2011-07-01 17:56:50 -0500193 // Does the $items array contain an id, quantity, price, and name? These are required
Andrey Andreev56454792012-05-17 14:32:19 +0300194 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000195 {
196 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
197 return FALSE;
198 }
199
200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200201
Andrey Andreevbb248832011-12-21 16:42:51 +0200202 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
Andrey Andreev17779d62011-12-22 13:21:08 +0200203 $items['qty'] = (float) $items['qty'];
Rick Ellis98783322009-02-17 02:29:44 +0000204
205 // If the quantity is zero or blank there's nothing for us to do
Andrey Andreev1a9b7e02012-11-01 16:23:47 +0200206 if ($items['qty'] == 0)
Rick Ellis98783322009-02-17 02:29:44 +0000207 {
208 return FALSE;
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Rick Ellis98783322009-02-17 02:29:44 +0000211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Rick Ellis98783322009-02-17 02:29:44 +0000213 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
214 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
215 // Note: These can be user-specified by setting the $this->product_id_rules variable.
Andrey Andreevbb248832011-12-21 16:42:51 +0200216 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
Rick Ellis98783322009-02-17 02:29:44 +0000217 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500218 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 +0000219 return FALSE;
220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Rick Ellis98783322009-02-17 02:29:44 +0000224 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
225 // Note: These can be user-specified by setting the $this->product_name_rules variable.
Louis Racicot025b6462013-03-07 09:32:16 -0500226 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000227 {
Andrew Seymour3dd66632011-12-13 15:50:52 +0000228 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');
229 return FALSE;
Rick Ellis98783322009-02-17 02:29:44 +0000230 }
231
232 // --------------------------------------------------------------------
233
Andrey Andreevbb248832011-12-21 16:42:51 +0200234 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
Andrey Andreev17779d62011-12-22 13:21:08 +0200235 $items['price'] = (float) $items['price'];
Barry Mienydd671972010-10-04 16:33:58 +0200236
Rick Ellis98783322009-02-17 02:29:44 +0000237 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200238 // Every time something is added to the cart it is stored in the master cart array.
239 // Each row in the cart array, however, must have a unique index that identifies not only
240 // a particular product, but makes it possible to store identical products with different options.
241 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500242 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000243 // both sizes because it's the same shirt. The only difference will be the size.
244 // Internally, we need to treat identical submissions, but with different options, as a unique product.
245 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
246 // This becomes the unique "row ID"
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300247 if (isset($items['options']) && count($items['options']) > 0)
Rick Ellis98783322009-02-17 02:29:44 +0000248 {
ThallisPHPf074dff2012-05-03 16:01:46 -0300249 $rowid = md5($items['id'].serialize($items['options']));
Rick Ellis98783322009-02-17 02:29:44 +0000250 }
251 else
252 {
253 // No options were submitted so we simply MD5 the product ID.
254 // Technically, we don't need to MD5 the ID in this case, but it makes
255 // sense to standardize the format of array indexes for both conditions
256 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200257 }
Rick Ellis98783322009-02-17 02:29:44 +0000258
259 // --------------------------------------------------------------------
260
261 // 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 +0000262 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200263 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreevbb248832011-12-21 16:42:51 +0200265 // Re-create the entry, just to make sure our index contains only the data from this submission
266 $items['rowid'] = $rowid;
267 $items['qty'] += $old_quantity;
268 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200269
Phil Sturgeon90910512011-07-20 10:07:40 -0600270 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000271 }
272
273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200274
Rick Ellis98783322009-02-17 02:29:44 +0000275 /**
276 * Update the cart
277 *
Barry Mienydd671972010-10-04 16:33:58 +0200278 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000279 * Typically it is called from the "view cart" page if a user makes
280 * changes to the quantity before checkout. That array must contain the
281 * product ID and quantity for each item.
282 *
Rick Ellis98783322009-02-17 02:29:44 +0000283 * @param array
Rick Ellis98783322009-02-17 02:29:44 +0000284 * @return bool
285 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200286 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000287 {
288 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200289 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000290 {
291 return FALSE;
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
294 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 // or multiple products using a multi-dimensional one. The way we
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200296 // determine the array type is by looking for a required array key named "rowid".
Rick Ellis98783322009-02-17 02:29:44 +0000297 // If it's not found we assume it's a multi-dimensional array
298 $save_cart = FALSE;
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200299 if (isset($items['rowid']))
Rick Ellis98783322009-02-17 02:29:44 +0000300 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200301 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000302 {
303 $save_cart = TRUE;
304 }
305 }
306 else
307 {
308 foreach ($items as $val)
309 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200310 if (is_array($val) && isset($val['rowid']))
Rick Ellis98783322009-02-17 02:29:44 +0000311 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200312 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000313 {
314 $save_cart = TRUE;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316 }
Rick Ellis98783322009-02-17 02:29:44 +0000317 }
318 }
319
320 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200321 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000322 {
323 $this->_save_cart();
324 return TRUE;
325 }
326
327 return FALSE;
328 }
329
330 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200331
Rick Ellis98783322009-02-17 02:29:44 +0000332 /**
333 * Update the cart
334 *
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200335 * This function permits changing item properties.
Rick Ellis98783322009-02-17 02:29:44 +0000336 * Typically it is called from the "view cart" page if a user makes
337 * changes to the quantity before checkout. That array must contain the
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200338 * rowid and quantity for each item.
Rick Ellis98783322009-02-17 02:29:44 +0000339 *
Rick Ellis98783322009-02-17 02:29:44 +0000340 * @param array
341 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200342 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300343 protected function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000344 {
345 // Without these array indexes there is nothing we can do
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200346 if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']]))
Rick Ellis98783322009-02-17 02:29:44 +0000347 {
348 return FALSE;
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Rick Ellis98783322009-02-17 02:29:44 +0000351 // Prep the quantity
Ahmad Anbar2702a3b2014-03-14 16:53:44 +0200352 if (isset($items['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000353 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200354 $items['qty'] = (float) $items['qty'];
355 // Is the quantity zero? If so we will remove the item from the cart.
356 // If the quantity is greater than zero we are updating
357 if ($items['qty'] == 0)
358 {
359 unset($this->_cart_contents[$items['rowid']]);
Ahmad Anbar9af07462014-03-14 15:33:18 +0200360 return TRUE;
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200361 }
Andrey Andreevdb3e49d2014-03-17 11:04:55 +0200362 }
363
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200364 // find updatable keys
365 $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items));
366 // if a price was passed, make sure it contains valid data
367 if (isset($items['price']))
Rick Ellis98783322009-02-17 02:29:44 +0000368 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200369 $items['price'] = (float) $items['price'];
Rick Ellis98783322009-02-17 02:29:44 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200372 // product id & name shouldn't be changed
373 foreach (array_diff($keys, array('id', 'name')) as $key)
374 {
375 $this->_cart_contents[$items['rowid']][$key] = $items[$key];
Andrey Andreevdb3e49d2014-03-17 11:04:55 +0200376 }
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200377
Rick Ellis98783322009-02-17 02:29:44 +0000378 return TRUE;
379 }
380
381 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200382
Rick Ellis98783322009-02-17 02:29:44 +0000383 /**
384 * Save the cart array to the session DB
385 *
Rick Ellis98783322009-02-17 02:29:44 +0000386 * @return bool
387 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300388 protected function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000389 {
vlakoff35672462013-02-15 01:36:04 +0100390 // Let's add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200391 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000392 foreach ($this->_cart_contents as $key => $val)
393 {
394 // We make sure the array contains the proper indexes
Andrey Andreev56454792012-05-17 14:32:19 +0300395 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000396 {
397 continue;
398 }
399
Andrey Andreevbb248832011-12-21 16:42:51 +0200400 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
401 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000402 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000403 }
404
Andrey Andreev56454792012-05-17 14:32:19 +0300405 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000406 if (count($this->_cart_contents) <= 2)
407 {
408 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200409
Rick Ellis98783322009-02-17 02:29:44 +0000410 // Nothing more to do... coffee time!
411 return FALSE;
412 }
413
414 // If we made it this far it means that our cart has data.
415 // Let's pass it to the Session class so it can be stored
416 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
417
Andrey Andreev17779d62011-12-22 13:21:08 +0200418 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200419 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000420 }
421
422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Rick Ellis98783322009-02-17 02:29:44 +0000424 /**
425 * Cart Total
426 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300427 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000428 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200429 public function total()
Rick Ellis98783322009-02-17 02:29:44 +0000430 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000431 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000432 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200433
Andrew Seymourf75ec112011-12-14 09:36:39 +0000434 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200435
Andrew Seymourf75ec112011-12-14 09:36:39 +0000436 /**
437 * Remove Item
438 *
439 * Removes an item from the cart
440 *
Timothy Warren86611db2012-04-27 10:06:25 -0400441 * @param int
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300442 * @return bool
Andrew Seymourf75ec112011-12-14 09:36:39 +0000443 */
444 public function remove($rowid)
445 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200446 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000447 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000448 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200449 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000450 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200451
Rick Ellis98783322009-02-17 02:29:44 +0000452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Rick Ellis98783322009-02-17 02:29:44 +0000454 /**
455 * Total Items
456 *
457 * Returns the total item count
458 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300459 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000460 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200461 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000462 {
463 return $this->_cart_contents['total_items'];
464 }
465
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Rick Ellis98783322009-02-17 02:29:44 +0000468 /**
469 * Cart Contents
470 *
471 * Returns the entire cart array
472 *
Timothy Warren86611db2012-04-27 10:06:25 -0400473 * @param bool
Rick Ellis98783322009-02-17 02:29:44 +0000474 * @return array
475 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200476 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000477 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000478 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200479 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200480
Rick Ellis98783322009-02-17 02:29:44 +0000481 // Remove these so they don't create a problem when showing the cart table
482 unset($cart['total_items']);
483 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200484
Rick Ellis98783322009-02-17 02:29:44 +0000485 return $cart;
486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Rick Ellis98783322009-02-17 02:29:44 +0000490 /**
Andrey Andreevcdeee662012-10-25 17:29:52 +0300491 * Get cart item
492 *
493 * Returns the details of a specific item in the cart
494 *
495 * @param string $row_id
496 * @return array
497 */
498 public function get_item($row_id)
499 {
500 return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->_cart_contents[$row_id]))
501 ? FALSE
502 : $this->_cart_contents[$row_id];
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Rick Ellis98783322009-02-17 02:29:44 +0000508 * Has options
509 *
510 * Returns TRUE if the rowid passed to this function correlates to an item
511 * that has options associated with it.
512 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300513 * @param string $row_id = ''
Andrey Andreevbb248832011-12-21 16:42:51 +0200514 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000515 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300516 public function has_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000517 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300518 return (isset($this->_cart_contents[$row_id]['options']) && count($this->_cart_contents[$row_id]['options']) !== 0);
Rick Ellis98783322009-02-17 02:29:44 +0000519 }
520
521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200522
Rick Ellis98783322009-02-17 02:29:44 +0000523 /**
524 * Product options
525 *
526 * Returns the an array of options, for a particular product row ID
527 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300528 * @param string $row_id = ''
Rick Ellis98783322009-02-17 02:29:44 +0000529 * @return array
530 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300531 public function product_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000532 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300533 return isset($this->_cart_contents[$row_id]['options']) ? $this->_cart_contents[$row_id]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000534 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000535
536 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200537
Rick Ellisdf39d512009-02-24 22:29:37 +0000538 /**
539 * Format Number
540 *
541 * Returns the supplied number with commas and a decimal point.
542 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300543 * @param float
Andrey Andreevbb248832011-12-21 16:42:51 +0200544 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000545 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200546 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000547 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100548 return ($n === '') ? '' : number_format( (float) $n, 2, '.', ',');
Rick Ellisdf39d512009-02-24 22:29:37 +0000549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Rick Ellis98783322009-02-17 02:29:44 +0000551 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200552
Rick Ellis98783322009-02-17 02:29:44 +0000553 /**
554 * Destroy the cart
555 *
556 * Empties the cart and kills the session
557 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200558 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000559 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200560 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000561 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200562 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000563 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000564 }
565
Rick Ellis98783322009-02-17 02:29:44 +0000566}
Rick Ellis98783322009-02-17 02:29:44 +0000567
568/* End of file Cart.php */
Louis Racicot65b8f832013-03-11 09:03:25 -0400569/* Location: ./system/libraries/Cart.php */