blob: 9c9a285cdead2e5ca3e986fbe5af34fd1ffc8165 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2006 - 2014, 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 */
Louis Racicot65b8f832013-03-11 09:03:25 -040054 public $product_name_rules = '\w \-\.\:';
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 Andreev56454792012-05-17 14:32:19 +030064
Timothy Warren86611db2012-04-27 10:06:25 -040065 /**
66 * Reference to CodeIgniter instance
67 *
68 * @var object
69 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030070 protected $CI;
Andrey Andreev56454792012-05-17 14:32:19 +030071
Timothy Warren86611db2012-04-27 10:06:25 -040072 /**
73 * Contents of the cart
74 *
75 * @var array
76 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +030077 protected $_cart_contents = array();
Rick Ellis98783322009-02-17 02:29:44 +000078
79 /**
80 * Shopping Class Constructor
81 *
82 * The constructor loads the Session class, used to store the shopping cart contents.
Timothy Warren86611db2012-04-27 10:06:25 -040083 *
84 * @param array
Andrey Andreev56454792012-05-17 14:32:19 +030085 * @return void
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
Edwin Awedc9afa2013-01-25 16:12:20 +080096 $this->CI->load->driver('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');
Ivan Tcholakov29453cd2012-11-23 13:54:28 +0200100 if ($this->_cart_contents === NULL)
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
Andrey Andreev56454792012-05-17 14:32:19 +0300183 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name']))
Rick Ellis98783322009-02-17 02:29:44 +0000184 {
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
Andrey Andreev1a9b7e02012-11-01 16:23:47 +0200195 if ($items['qty'] == 0)
Rick Ellis98783322009-02-17 02:29:44 +0000196 {
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.
Louis Racicot025b6462013-03-07 09:32:16 -0500215 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $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 // We now need to create a unique identifier for the item being inserted into the cart.
Barry Mienydd671972010-10-04 16:33:58 +0200227 // Every time something is added to the cart it is stored in the master cart array.
228 // Each row in the cart array, however, must have a unique index that identifies not only
229 // a particular product, but makes it possible to store identical products with different options.
230 // For example, what if someone buys two identical t-shirts (same product ID), but in
Derek Jones37f4b9c2011-07-01 17:56:50 -0500231 // different sizes? The product ID (and other attributes, like the name) will be identical for
Rick Ellis98783322009-02-17 02:29:44 +0000232 // both sizes because it's the same shirt. The only difference will be the size.
233 // Internally, we need to treat identical submissions, but with different options, as a unique product.
234 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
235 // This becomes the unique "row ID"
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300236 if (isset($items['options']) && count($items['options']) > 0)
Rick Ellis98783322009-02-17 02:29:44 +0000237 {
ThallisPHPf074dff2012-05-03 16:01:46 -0300238 $rowid = md5($items['id'].serialize($items['options']));
Rick Ellis98783322009-02-17 02:29:44 +0000239 }
240 else
241 {
242 // No options were submitted so we simply MD5 the product ID.
243 // Technically, we don't need to MD5 the ID in this case, but it makes
244 // sense to standardize the format of array indexes for both conditions
245 $rowid = md5($items['id']);
Barry Mienydd671972010-10-04 16:33:58 +0200246 }
Rick Ellis98783322009-02-17 02:29:44 +0000247
248 // --------------------------------------------------------------------
249
250 // 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 +0000251 // grab quantity if it's already there and add it on
Andrey Andreevbb248832011-12-21 16:42:51 +0200252 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200253
Andrey Andreevbb248832011-12-21 16:42:51 +0200254 // Re-create the entry, just to make sure our index contains only the data from this submission
255 $items['rowid'] = $rowid;
256 $items['qty'] += $old_quantity;
257 $this->_cart_contents[$rowid] = $items;
Barry Mienydd671972010-10-04 16:33:58 +0200258
Phil Sturgeon90910512011-07-20 10:07:40 -0600259 return $rowid;
Rick Ellis98783322009-02-17 02:29:44 +0000260 }
261
262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200263
Rick Ellis98783322009-02-17 02:29:44 +0000264 /**
265 * Update the cart
266 *
Barry Mienydd671972010-10-04 16:33:58 +0200267 * This function permits the quantity of a given item to be changed.
Rick Ellis98783322009-02-17 02:29:44 +0000268 * Typically it is called from the "view cart" page if a user makes
269 * changes to the quantity before checkout. That array must contain the
270 * product ID and quantity for each item.
271 *
Rick Ellis98783322009-02-17 02:29:44 +0000272 * @param array
Rick Ellis98783322009-02-17 02:29:44 +0000273 * @return bool
274 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200275 public function update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000276 {
277 // Was any cart data passed?
Andrey Andreevbb248832011-12-21 16:42:51 +0200278 if ( ! is_array($items) OR count($items) === 0)
Rick Ellis98783322009-02-17 02:29:44 +0000279 {
280 return FALSE;
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
283 // You can either update a single product using a one-dimensional array,
Derek Jones37f4b9c2011-07-01 17:56:50 -0500284 // or multiple products using a multi-dimensional one. The way we
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200285 // determine the array type is by looking for a required array key named "rowid".
Rick Ellis98783322009-02-17 02:29:44 +0000286 // If it's not found we assume it's a multi-dimensional array
287 $save_cart = FALSE;
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200288 if (isset($items['rowid']))
Rick Ellis98783322009-02-17 02:29:44 +0000289 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200290 if ($this->_update($items) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000291 {
292 $save_cart = TRUE;
293 }
294 }
295 else
296 {
297 foreach ($items as $val)
298 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200299 if (is_array($val) && isset($val['rowid']))
Rick Ellis98783322009-02-17 02:29:44 +0000300 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200301 if ($this->_update($val) === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000302 {
303 $save_cart = TRUE;
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305 }
Rick Ellis98783322009-02-17 02:29:44 +0000306 }
307 }
308
309 // Save the cart data if the insert was successful
Andrey Andreevbb248832011-12-21 16:42:51 +0200310 if ($save_cart === TRUE)
Rick Ellis98783322009-02-17 02:29:44 +0000311 {
312 $this->_save_cart();
313 return TRUE;
314 }
315
316 return FALSE;
317 }
318
319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Rick Ellis98783322009-02-17 02:29:44 +0000321 /**
322 * Update the cart
323 *
Ahmad Anbar7d16de62014-02-13 01:45:27 +0200324 * This function permits changing item properties.
Rick Ellis98783322009-02-17 02:29:44 +0000325 * Typically it is called from the "view cart" page if a user makes
326 * changes to the quantity before checkout. That array must contain the
Ahmad Anbarda2bdf22014-02-13 12:59:18 +0200327 * rowid and quantity for each item.
Rick Ellis98783322009-02-17 02:29:44 +0000328 *
Rick Ellis98783322009-02-17 02:29:44 +0000329 * @param array
330 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300332 protected function _update($items = array())
Rick Ellis98783322009-02-17 02:29:44 +0000333 {
334 // Without these array indexes there is nothing we can do
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200335 if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']]))
Rick Ellis98783322009-02-17 02:29:44 +0000336 {
337 return FALSE;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Rick Ellis98783322009-02-17 02:29:44 +0000340 // Prep the quantity
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200341 if ( isset($items['qty']) )
Rick Ellis98783322009-02-17 02:29:44 +0000342 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200343 $items['qty'] = (float) $items['qty'];
344 // Is the quantity zero? If so we will remove the item from the cart.
345 // If the quantity is greater than zero we are updating
346 if ($items['qty'] == 0)
347 {
348 unset($this->_cart_contents[$items['rowid']]);
Ahmad Anbar9af07462014-03-14 15:33:18 +0200349 return TRUE;
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200350 }
351 }
352
353 // find updatable keys
354 $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items));
355 // if a price was passed, make sure it contains valid data
356 if (isset($items['price']))
Rick Ellis98783322009-02-17 02:29:44 +0000357 {
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200358 $items['price'] = (float) $items['price'];
Rick Ellis98783322009-02-17 02:29:44 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Ahmad Anbar4321cb02014-03-14 15:21:13 +0200361 // product id & name shouldn't be changed
362 foreach (array_diff($keys, array('id', 'name')) as $key)
363 {
364 $this->_cart_contents[$items['rowid']][$key] = $items[$key];
365 }
366
Rick Ellis98783322009-02-17 02:29:44 +0000367 return TRUE;
368 }
369
370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Rick Ellis98783322009-02-17 02:29:44 +0000372 /**
373 * Save the cart array to the session DB
374 *
Rick Ellis98783322009-02-17 02:29:44 +0000375 * @return bool
376 */
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300377 protected function _save_cart()
Rick Ellis98783322009-02-17 02:29:44 +0000378 {
vlakoff35672462013-02-15 01:36:04 +0100379 // Let's add up the individual prices and set the cart sub-total
Andrey Andreevbb248832011-12-21 16:42:51 +0200380 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
Rick Ellis98783322009-02-17 02:29:44 +0000381 foreach ($this->_cart_contents as $key => $val)
382 {
383 // We make sure the array contains the proper indexes
Andrey Andreev56454792012-05-17 14:32:19 +0300384 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
Rick Ellis98783322009-02-17 02:29:44 +0000385 {
386 continue;
387 }
388
Andrey Andreevbb248832011-12-21 16:42:51 +0200389 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
390 $this->_cart_contents['total_items'] += $val['qty'];
Rick Ellisdf39d512009-02-24 22:29:37 +0000391 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
Rick Ellis98783322009-02-17 02:29:44 +0000392 }
393
Andrey Andreev56454792012-05-17 14:32:19 +0300394 // Is our cart empty? If so we delete it from the session
Rick Ellis98783322009-02-17 02:29:44 +0000395 if (count($this->_cart_contents) <= 2)
396 {
397 $this->CI->session->unset_userdata('cart_contents');
Barry Mienydd671972010-10-04 16:33:58 +0200398
Rick Ellis98783322009-02-17 02:29:44 +0000399 // Nothing more to do... coffee time!
400 return FALSE;
401 }
402
403 // If we made it this far it means that our cart has data.
404 // Let's pass it to the Session class so it can be stored
405 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
406
Andrey Andreev17779d62011-12-22 13:21:08 +0200407 // Woot!
Barry Mienydd671972010-10-04 16:33:58 +0200408 return TRUE;
Rick Ellis98783322009-02-17 02:29:44 +0000409 }
410
411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Rick Ellis98783322009-02-17 02:29:44 +0000413 /**
414 * Cart Total
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()
Rick Ellis98783322009-02-17 02:29:44 +0000419 {
Rick Ellisdf39d512009-02-24 22:29:37 +0000420 return $this->_cart_contents['cart_total'];
Rick Ellis98783322009-02-17 02:29:44 +0000421 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200422
Andrew Seymourf75ec112011-12-14 09:36:39 +0000423 // --------------------------------------------------------------------
Andrey Andreevbb248832011-12-21 16:42:51 +0200424
Andrew Seymourf75ec112011-12-14 09:36:39 +0000425 /**
426 * Remove Item
427 *
428 * Removes an item from the cart
429 *
Timothy Warren86611db2012-04-27 10:06:25 -0400430 * @param int
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300431 * @return bool
Andrew Seymourf75ec112011-12-14 09:36:39 +0000432 */
433 public function remove($rowid)
434 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200435 // unset & save
Andrew Seymourf75ec112011-12-14 09:36:39 +0000436 unset($this->_cart_contents[$rowid]);
Andrew Seymourf75ec112011-12-14 09:36:39 +0000437 $this->_save_cart();
Andrey Andreevbb248832011-12-21 16:42:51 +0200438 return TRUE;
Andrew Seymourf75ec112011-12-14 09:36:39 +0000439 }
Andrey Andreevbb248832011-12-21 16:42:51 +0200440
Rick Ellis98783322009-02-17 02:29:44 +0000441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Rick Ellis98783322009-02-17 02:29:44 +0000443 /**
444 * Total Items
445 *
446 * Returns the total item count
447 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300448 * @return int
Rick Ellis98783322009-02-17 02:29:44 +0000449 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200450 public function total_items()
Rick Ellis98783322009-02-17 02:29:44 +0000451 {
452 return $this->_cart_contents['total_items'];
453 }
454
455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Rick Ellis98783322009-02-17 02:29:44 +0000457 /**
458 * Cart Contents
459 *
460 * Returns the entire cart array
461 *
Timothy Warren86611db2012-04-27 10:06:25 -0400462 * @param bool
Rick Ellis98783322009-02-17 02:29:44 +0000463 * @return array
464 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200465 public function contents($newest_first = FALSE)
Rick Ellis98783322009-02-17 02:29:44 +0000466 {
Andrew Seymourde2e96a2011-12-13 16:44:59 +0000467 // do we want the newest first?
Andrey Andreevbb248832011-12-21 16:42:51 +0200468 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
Barry Mienydd671972010-10-04 16:33:58 +0200469
Rick Ellis98783322009-02-17 02:29:44 +0000470 // Remove these so they don't create a problem when showing the cart table
471 unset($cart['total_items']);
472 unset($cart['cart_total']);
Barry Mienydd671972010-10-04 16:33:58 +0200473
Rick Ellis98783322009-02-17 02:29:44 +0000474 return $cart;
475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Rick Ellis98783322009-02-17 02:29:44 +0000479 /**
Andrey Andreevcdeee662012-10-25 17:29:52 +0300480 * Get cart item
481 *
482 * Returns the details of a specific item in the cart
483 *
484 * @param string $row_id
485 * @return array
486 */
487 public function get_item($row_id)
488 {
489 return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->_cart_contents[$row_id]))
490 ? FALSE
491 : $this->_cart_contents[$row_id];
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
Rick Ellis98783322009-02-17 02:29:44 +0000497 * Has options
498 *
499 * Returns TRUE if the rowid passed to this function correlates to an item
500 * that has options associated with it.
501 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300502 * @param string $row_id = ''
Andrey Andreevbb248832011-12-21 16:42:51 +0200503 * @return bool
Rick Ellis98783322009-02-17 02:29:44 +0000504 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300505 public function has_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000506 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300507 return (isset($this->_cart_contents[$row_id]['options']) && count($this->_cart_contents[$row_id]['options']) !== 0);
Rick Ellis98783322009-02-17 02:29:44 +0000508 }
509
510 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200511
Rick Ellis98783322009-02-17 02:29:44 +0000512 /**
513 * Product options
514 *
515 * Returns the an array of options, for a particular product row ID
516 *
Andrey Andreevcdeee662012-10-25 17:29:52 +0300517 * @param string $row_id = ''
Rick Ellis98783322009-02-17 02:29:44 +0000518 * @return array
519 */
Andrey Andreevcdeee662012-10-25 17:29:52 +0300520 public function product_options($row_id = '')
Rick Ellis98783322009-02-17 02:29:44 +0000521 {
Andrey Andreevcdeee662012-10-25 17:29:52 +0300522 return isset($this->_cart_contents[$row_id]['options']) ? $this->_cart_contents[$row_id]['options'] : array();
Rick Ellis98783322009-02-17 02:29:44 +0000523 }
Rick Ellisdf39d512009-02-24 22:29:37 +0000524
525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200526
Rick Ellisdf39d512009-02-24 22:29:37 +0000527 /**
528 * Format Number
529 *
530 * Returns the supplied number with commas and a decimal point.
531 *
Andrey Andreev6f042cc2012-03-26 14:58:33 +0300532 * @param float
Andrey Andreevbb248832011-12-21 16:42:51 +0200533 * @return string
Rick Ellisdf39d512009-02-24 22:29:37 +0000534 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200535 public function format_number($n = '')
Rick Ellisdf39d512009-02-24 22:29:37 +0000536 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100537 return ($n === '') ? '' : number_format( (float) $n, 2, '.', ',');
Rick Ellisdf39d512009-02-24 22:29:37 +0000538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Rick Ellis98783322009-02-17 02:29:44 +0000540 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200541
Rick Ellis98783322009-02-17 02:29:44 +0000542 /**
543 * Destroy the cart
544 *
545 * Empties the cart and kills the session
546 *
Andrey Andreevbb248832011-12-21 16:42:51 +0200547 * @return void
Rick Ellis98783322009-02-17 02:29:44 +0000548 */
Andrey Andreevbb248832011-12-21 16:42:51 +0200549 public function destroy()
Rick Ellis98783322009-02-17 02:29:44 +0000550 {
Andrey Andreevbb248832011-12-21 16:42:51 +0200551 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
Rick Ellis9c86ce52009-02-17 19:54:14 +0000552 $this->CI->session->unset_userdata('cart_contents');
Rick Ellis98783322009-02-17 02:29:44 +0000553 }
554
Rick Ellis98783322009-02-17 02:29:44 +0000555}
Rick Ellis98783322009-02-17 02:29:44 +0000556
557/* End of file Cart.php */
Louis Racicot65b8f832013-03-11 09:03:25 -0400558/* Location: ./system/libraries/Cart.php */