Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * 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 Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Shopping Cart Class |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Shopping Cart |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/cart.html |
| 38 | */ |
| 39 | class CI_Cart { |
| 40 | |
| 41 | // These are the regular expression rules that we use to validate the product ID and product name |
| 42 | var $product_id_rules = '\.a-z0-9_-'; // alpha-numeric, dashes, underscores, or periods |
| 43 | var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame] | 44 | var $product_name_safe = true; // only allow safe product names |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 46 | // Private variables. Do not change! |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 47 | var $CI; |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 48 | var $_cart_contents = array(); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | /** |
| 52 | * Shopping Class Constructor |
| 53 | * |
| 54 | * The constructor loads the Session class, used to store the shopping cart contents. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 56 | public function __construct($params = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 57 | { |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 58 | // Set the super object to a local variable for use later |
| 59 | $this->CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 61 | // Are any config settings being passed manually? If so, set them |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 62 | $config = array(); |
| 63 | if (count($params) > 0) |
| 64 | { |
| 65 | foreach ($params as $key => $val) |
| 66 | { |
| 67 | $config[$key] = $val; |
| 68 | } |
| 69 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 71 | // Load the Sessions class |
| 72 | $this->CI->load->library('session', $config); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 74 | // Grab the shopping cart array from the session table, if it exists |
| 75 | if ($this->CI->session->userdata('cart_contents') !== FALSE) |
| 76 | { |
| 77 | $this->_cart_contents = $this->CI->session->userdata('cart_contents'); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | // No cart exists so we'll set some base values |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | $this->_cart_contents['cart_total'] = 0; |
| 83 | $this->_cart_contents['total_items'] = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 84 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 86 | log_message('debug', "Cart Class Initialized"); |
| 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Insert items into the cart and save it to the session table |
| 93 | * |
| 94 | * @access public |
| 95 | * @param array |
| 96 | * @return bool |
| 97 | */ |
| 98 | function insert($items = array()) |
| 99 | { |
| 100 | // Was any cart data passed? No? Bah... |
| 101 | if ( ! is_array($items) OR count($items) == 0) |
| 102 | { |
| 103 | log_message('error', 'The insert method must be passed an array containing data.'); |
| 104 | return FALSE; |
| 105 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
| 107 | // You can either insert a single product using a one-dimensional array, |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 108 | // or multiple products using a multi-dimensional one. The way we |
| 109 | // determine the array type is by looking for a required array key named "id" |
| 110 | // at the top level. If it's not found, we will assume it's a multi-dimensional array. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
| 112 | $save_cart = FALSE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 113 | if (isset($items['id'])) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | { |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 115 | if (($rowid = $this->_insert($items))) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 116 | { |
| 117 | $save_cart = TRUE; |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | foreach ($items as $val) |
| 123 | { |
| 124 | if (is_array($val) AND isset($val['id'])) |
| 125 | { |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 126 | if ($this->_insert($val)) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 127 | { |
| 128 | $save_cart = TRUE; |
| 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | // Save the cart data if the insert was successful |
| 135 | if ($save_cart == TRUE) |
| 136 | { |
| 137 | $this->_save_cart(); |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 138 | return isset($rowid) ? $rowid : TRUE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return FALSE; |
| 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 146 | /** |
| 147 | * Insert |
| 148 | * |
| 149 | * @access private |
| 150 | * @param array |
| 151 | * @return bool |
| 152 | */ |
| 153 | function _insert($items = array()) |
| 154 | { |
| 155 | // Was any cart data passed? No? Bah... |
| 156 | if ( ! is_array($items) OR count($items) == 0) |
| 157 | { |
| 158 | log_message('error', 'The insert method must be passed an array containing data.'); |
| 159 | return FALSE; |
| 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 162 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 164 | // Does the $items array contain an id, quantity, price, and name? These are required |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 165 | if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) |
| 166 | { |
| 167 | log_message('error', 'The cart array must contain a product ID, quantity, price, and name.'); |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 173 | // Prep the quantity. It can only be a number. Duh... |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 174 | $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty'])); |
| 175 | // Trim any leading zeros |
| 176 | $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty'])); |
| 177 | |
| 178 | // If the quantity is zero or blank there's nothing for us to do |
| 179 | if ( ! is_numeric($items['qty']) OR $items['qty'] == 0) |
| 180 | { |
| 181 | return FALSE; |
| 182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 184 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 186 | // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods |
| 187 | // Not totally sure we should impose this rule, but it seems prudent to standardize IDs. |
| 188 | // Note: These can be user-specified by setting the $this->product_id_rules variable. |
| 189 | if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id'])) |
| 190 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 191 | log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 192 | return FALSE; |
| 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 197 | // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods. |
| 198 | // Note: These can be user-specified by setting the $this->product_name_rules variable. |
Andrew Seymour | 3dd6663 | 2011-12-13 15:50:52 +0000 | [diff] [blame] | 199 | if ( $this->product_name_safe && ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 200 | { |
Andrew Seymour | 3dd6663 | 2011-12-13 15:50:52 +0000 | [diff] [blame] | 201 | 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'); |
| 202 | return FALSE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 207 | // Prep the price. Remove anything that isn't a number or decimal point. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 208 | $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price'])); |
| 209 | // Trim any leading zeros |
| 210 | $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price'])); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 212 | // Is the price a valid number? |
| 213 | if ( ! is_numeric($items['price'])) |
| 214 | { |
| 215 | log_message('error', 'An invalid price was submitted for product ID: '.$items['id']); |
| 216 | return FALSE; |
| 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 221 | // We now need to create a unique identifier for the item being inserted into the cart. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | // Every time something is added to the cart it is stored in the master cart array. |
| 223 | // Each row in the cart array, however, must have a unique index that identifies not only |
| 224 | // a particular product, but makes it possible to store identical products with different options. |
| 225 | // For example, what if someone buys two identical t-shirts (same product ID), but in |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 226 | // different sizes? The product ID (and other attributes, like the name) will be identical for |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 227 | // both sizes because it's the same shirt. The only difference will be the size. |
| 228 | // Internally, we need to treat identical submissions, but with different options, as a unique product. |
| 229 | // Our solution is to convert the options array to a string and MD5 it along with the product ID. |
| 230 | // This becomes the unique "row ID" |
| 231 | if (isset($items['options']) AND count($items['options']) > 0) |
| 232 | { |
| 233 | $rowid = md5($items['id'].implode('', $items['options'])); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | // No options were submitted so we simply MD5 the product ID. |
| 238 | // Technically, we don't need to MD5 the ID in this case, but it makes |
| 239 | // sense to standardize the format of array indexes for both conditions |
| 240 | $rowid = md5($items['id']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 242 | |
| 243 | // -------------------------------------------------------------------- |
| 244 | |
| 245 | // Now that we have our unique "row ID", we'll add our cart items to the master array |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame] | 246 | // grab quantity if it's already there and add it on |
Andrew Seymour | 17aebce | 2011-12-13 16:57:13 +0000 | [diff] [blame] | 247 | if (isset($this->_cart_contents[$rowid]['qty'])) |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame] | 248 | { |
| 249 | // set our old quantity |
| 250 | $old_quantity = (int)$this->_cart_contents[$rowid]['qty']; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | // we have no old quantity but - we don't want to throw an error |
| 255 | $old_quantity = 0; |
| 256 | } |
| 257 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 258 | // let's unset this first, just to make sure our index contains only the data from this submission |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | unset($this->_cart_contents[$rowid]); |
| 260 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 261 | // Create a new index with our new row ID |
| 262 | $this->_cart_contents[$rowid]['rowid'] = $rowid; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
| 264 | // And add the new items to the cart array |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 265 | foreach ($items as $key => $val) |
| 266 | { |
| 267 | $this->_cart_contents[$rowid][$key] = $val; |
| 268 | } |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame] | 269 | |
| 270 | // add old quantity back in |
| 271 | $this->_cart_contents[$rowid]['qty'] = ($this->_cart_contents[$rowid]['qty'] + $old_quantity); |
| 272 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 273 | // Woot! |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 274 | return $rowid; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 279 | /** |
| 280 | * Update the cart |
| 281 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | * This function permits the quantity of a given item to be changed. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 283 | * Typically it is called from the "view cart" page if a user makes |
| 284 | * changes to the quantity before checkout. That array must contain the |
| 285 | * product ID and quantity for each item. |
| 286 | * |
| 287 | * @access public |
| 288 | * @param array |
| 289 | * @param string |
| 290 | * @return bool |
| 291 | */ |
| 292 | function update($items = array()) |
| 293 | { |
| 294 | // Was any cart data passed? |
| 295 | if ( ! is_array($items) OR count($items) == 0) |
| 296 | { |
| 297 | return FALSE; |
| 298 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
| 300 | // You can either update a single product using a one-dimensional array, |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 301 | // or multiple products using a multi-dimensional one. The way we |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 302 | // determine the array type is by looking for a required array key named "id". |
| 303 | // If it's not found we assume it's a multi-dimensional array |
| 304 | $save_cart = FALSE; |
| 305 | if (isset($items['rowid']) AND isset($items['qty'])) |
| 306 | { |
| 307 | if ($this->_update($items) == TRUE) |
| 308 | { |
| 309 | $save_cart = TRUE; |
| 310 | } |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | foreach ($items as $val) |
| 315 | { |
| 316 | if (is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) |
| 317 | { |
| 318 | if ($this->_update($val) == TRUE) |
| 319 | { |
| 320 | $save_cart = TRUE; |
| 321 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | // Save the cart data if the insert was successful |
| 327 | if ($save_cart == TRUE) |
| 328 | { |
| 329 | $this->_save_cart(); |
| 330 | return TRUE; |
| 331 | } |
| 332 | |
| 333 | return FALSE; |
| 334 | } |
| 335 | |
| 336 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 338 | /** |
| 339 | * Update the cart |
| 340 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | * This function permits the quantity of a given item to be changed. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 342 | * Typically it is called from the "view cart" page if a user makes |
| 343 | * changes to the quantity before checkout. That array must contain the |
| 344 | * product ID and quantity for each item. |
| 345 | * |
| 346 | * @access private |
| 347 | * @param array |
| 348 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | */ |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 350 | function _update($items = array()) |
| 351 | { |
| 352 | // Without these array indexes there is nothing we can do |
| 353 | if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']])) |
| 354 | { |
| 355 | return FALSE; |
| 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 358 | // Prep the quantity |
| 359 | $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']); |
| 360 | |
| 361 | // Is the quantity a number? |
| 362 | if ( ! is_numeric($items['qty'])) |
| 363 | { |
| 364 | return FALSE; |
| 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 367 | // Is the new quantity different than what is already saved in the cart? |
| 368 | // If it's the same there's nothing to do |
| 369 | if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) |
| 370 | { |
| 371 | return FALSE; |
| 372 | } |
| 373 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 374 | // Is the quantity zero? If so we will remove the item from the cart. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 375 | // If the quantity is greater than zero we are updating |
| 376 | if ($items['qty'] == 0) |
| 377 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | unset($this->_cart_contents[$items['rowid']]); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
| 382 | $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; |
| 383 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 385 | return TRUE; |
| 386 | } |
| 387 | |
| 388 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 389 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 390 | /** |
| 391 | * Save the cart array to the session DB |
| 392 | * |
| 393 | * @access private |
| 394 | * @return bool |
| 395 | */ |
| 396 | function _save_cart() |
| 397 | { |
Rick Ellis | af4fb22 | 2009-02-24 22:44:22 +0000 | [diff] [blame] | 398 | // Unset these so our total can be calculated correctly below |
| 399 | unset($this->_cart_contents['total_items']); |
| 400 | unset($this->_cart_contents['cart_total']); |
| 401 | |
| 402 | // Lets add up the individual prices and set the cart sub-total |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 403 | $total = 0; |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 404 | $items = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 405 | foreach ($this->_cart_contents as $key => $val) |
| 406 | { |
| 407 | // We make sure the array contains the proper indexes |
Rick Ellis | af4fb22 | 2009-02-24 22:44:22 +0000 | [diff] [blame] | 408 | if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 409 | { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | $total += ($val['price'] * $val['qty']); |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 414 | $items += $val['qty']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 415 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 416 | // Set the subtotal |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 417 | $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 420 | // Set the cart total and total items. |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 421 | $this->_cart_contents['total_items'] = $items; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 422 | $this->_cart_contents['cart_total'] = $total; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 424 | // Is our cart empty? If so we delete it from the session |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 425 | if (count($this->_cart_contents) <= 2) |
| 426 | { |
| 427 | $this->CI->session->unset_userdata('cart_contents'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 429 | // Nothing more to do... coffee time! |
| 430 | return FALSE; |
| 431 | } |
| 432 | |
| 433 | // If we made it this far it means that our cart has data. |
| 434 | // Let's pass it to the Session class so it can be stored |
| 435 | $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents)); |
| 436 | |
| 437 | // Woot! |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | return TRUE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 443 | /** |
| 444 | * Cart Total |
| 445 | * |
| 446 | * @access public |
| 447 | * @return integer |
| 448 | */ |
| 449 | function total() |
| 450 | { |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 451 | return $this->_cart_contents['cart_total']; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 452 | } |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame^] | 453 | |
| 454 | // -------------------------------------------------------------------- |
| 455 | |
| 456 | /** |
| 457 | * Remove Item |
| 458 | * |
| 459 | * Removes an item from the cart |
| 460 | * |
| 461 | * @access public |
| 462 | * @return boolean |
| 463 | */ |
| 464 | public function remove($rowid) |
| 465 | { |
| 466 | // just do an unset |
| 467 | unset($this->_cart_contents[$rowid]); |
| 468 | |
| 469 | // we need to save the cart now we've made our changes |
| 470 | $this->_save_cart(); |
| 471 | |
| 472 | // completed |
| 473 | return true; |
| 474 | } |
| 475 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 476 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 478 | /** |
| 479 | * Total Items |
| 480 | * |
| 481 | * Returns the total item count |
| 482 | * |
| 483 | * @access public |
| 484 | * @return integer |
| 485 | */ |
| 486 | function total_items() |
| 487 | { |
| 488 | return $this->_cart_contents['total_items']; |
| 489 | } |
| 490 | |
| 491 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 493 | /** |
| 494 | * Cart Contents |
| 495 | * |
| 496 | * Returns the entire cart array |
| 497 | * |
| 498 | * @access public |
| 499 | * @return array |
| 500 | */ |
Andrew Seymour | de2e96a | 2011-12-13 16:44:59 +0000 | [diff] [blame] | 501 | function contents($newest_first = false) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 502 | { |
Andrew Seymour | de2e96a | 2011-12-13 16:44:59 +0000 | [diff] [blame] | 503 | // do we want the newest first? |
| 504 | if($newest_first) |
| 505 | { |
| 506 | // reverse the array |
| 507 | $cart = array_reverse($this->_cart_contents); |
| 508 | } else { |
| 509 | // just added first to last |
| 510 | $cart = $this->_cast_contents; |
| 511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 513 | // Remove these so they don't create a problem when showing the cart table |
| 514 | unset($cart['total_items']); |
| 515 | unset($cart['cart_total']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 517 | return $cart; |
| 518 | } |
| 519 | |
| 520 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Has options |
| 524 | * |
| 525 | * Returns TRUE if the rowid passed to this function correlates to an item |
| 526 | * that has options associated with it. |
| 527 | * |
| 528 | * @access public |
| 529 | * @return array |
| 530 | */ |
| 531 | function has_options($rowid = '') |
| 532 | { |
| 533 | if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) |
| 534 | { |
| 535 | return FALSE; |
| 536 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 537 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 538 | return TRUE; |
| 539 | } |
| 540 | |
| 541 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 542 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 543 | /** |
| 544 | * Product options |
| 545 | * |
| 546 | * Returns the an array of options, for a particular product row ID |
| 547 | * |
| 548 | * @access public |
| 549 | * @return array |
| 550 | */ |
| 551 | function product_options($rowid = '') |
| 552 | { |
| 553 | if ( ! isset($this->_cart_contents[$rowid]['options'])) |
| 554 | { |
| 555 | return array(); |
| 556 | } |
| 557 | |
| 558 | return $this->_cart_contents[$rowid]['options']; |
| 559 | } |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 560 | |
| 561 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 562 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 563 | /** |
| 564 | * Format Number |
| 565 | * |
| 566 | * Returns the supplied number with commas and a decimal point. |
| 567 | * |
| 568 | * @access public |
| 569 | * @return integer |
| 570 | */ |
| 571 | function format_number($n = '') |
| 572 | { |
| 573 | if ($n == '') |
| 574 | { |
| 575 | return ''; |
| 576 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 578 | // Remove anything that isn't a number or decimal point. |
| 579 | $n = trim(preg_replace('/([^0-9\.])/i', '', $n)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 580 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 581 | return number_format($n, 2, '.', ','); |
| 582 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 583 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 584 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 585 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 586 | /** |
| 587 | * Destroy the cart |
| 588 | * |
| 589 | * Empties the cart and kills the session |
| 590 | * |
| 591 | * @access public |
| 592 | * @return null |
| 593 | */ |
| 594 | function destroy() |
| 595 | { |
| 596 | unset($this->_cart_contents); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 597 | |
| 598 | $this->_cart_contents['cart_total'] = 0; |
| 599 | $this->_cart_contents['total_items'] = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 600 | |
Rick Ellis | 9c86ce5 | 2009-02-17 19:54:14 +0000 | [diff] [blame] | 601 | $this->CI->session->unset_userdata('cart_contents'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | |
| 605 | } |
| 606 | // END Cart Class |
| 607 | |
| 608 | /* End of file Cart.php */ |
| 609 | /* Location: ./system/libraries/Cart.php */ |