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 | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame^] | 199 | if($this->product_name_safe) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 200 | { |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame^] | 201 | if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name'])) |
| 202 | { |
| 203 | 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'); |
| 204 | return FALSE; |
| 205 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // -------------------------------------------------------------------- |
| 209 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 210 | // 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] | 211 | $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price'])); |
| 212 | // Trim any leading zeros |
| 213 | $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price'])); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 215 | // Is the price a valid number? |
| 216 | if ( ! is_numeric($items['price'])) |
| 217 | { |
| 218 | log_message('error', 'An invalid price was submitted for product ID: '.$items['id']); |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 224 | // 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] | 225 | // Every time something is added to the cart it is stored in the master cart array. |
| 226 | // Each row in the cart array, however, must have a unique index that identifies not only |
| 227 | // a particular product, but makes it possible to store identical products with different options. |
| 228 | // 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] | 229 | // 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] | 230 | // both sizes because it's the same shirt. The only difference will be the size. |
| 231 | // Internally, we need to treat identical submissions, but with different options, as a unique product. |
| 232 | // Our solution is to convert the options array to a string and MD5 it along with the product ID. |
| 233 | // This becomes the unique "row ID" |
| 234 | if (isset($items['options']) AND count($items['options']) > 0) |
| 235 | { |
| 236 | $rowid = md5($items['id'].implode('', $items['options'])); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | // No options were submitted so we simply MD5 the product ID. |
| 241 | // Technically, we don't need to MD5 the ID in this case, but it makes |
| 242 | // sense to standardize the format of array indexes for both conditions |
| 243 | $rowid = md5($items['id']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 245 | |
| 246 | // -------------------------------------------------------------------- |
| 247 | |
| 248 | // 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^] | 249 | // grab quantity if it's already there and add it on |
| 250 | if(isset($this->_cart_contents[$rowid]['qty'])) |
| 251 | { |
| 252 | // set our old quantity |
| 253 | $old_quantity = (int)$this->_cart_contents[$rowid]['qty']; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | // we have no old quantity but - we don't want to throw an error |
| 258 | $old_quantity = 0; |
| 259 | } |
| 260 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 261 | // 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] | 262 | unset($this->_cart_contents[$rowid]); |
| 263 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 264 | // Create a new index with our new row ID |
| 265 | $this->_cart_contents[$rowid]['rowid'] = $rowid; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
| 267 | // And add the new items to the cart array |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 268 | foreach ($items as $key => $val) |
| 269 | { |
| 270 | $this->_cart_contents[$rowid][$key] = $val; |
| 271 | } |
Andrew Seymour | 2e5bda3 | 2011-12-13 11:40:15 +0000 | [diff] [blame^] | 272 | |
| 273 | // add old quantity back in |
| 274 | $this->_cart_contents[$rowid]['qty'] = ($this->_cart_contents[$rowid]['qty'] + $old_quantity); |
| 275 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 276 | // Woot! |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 277 | return $rowid; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 281 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 282 | /** |
| 283 | * Update the cart |
| 284 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | * This function permits the quantity of a given item to be changed. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 286 | * Typically it is called from the "view cart" page if a user makes |
| 287 | * changes to the quantity before checkout. That array must contain the |
| 288 | * product ID and quantity for each item. |
| 289 | * |
| 290 | * @access public |
| 291 | * @param array |
| 292 | * @param string |
| 293 | * @return bool |
| 294 | */ |
| 295 | function update($items = array()) |
| 296 | { |
| 297 | // Was any cart data passed? |
| 298 | if ( ! is_array($items) OR count($items) == 0) |
| 299 | { |
| 300 | return FALSE; |
| 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
| 303 | // You can either update a single product using a one-dimensional array, |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 304 | // or multiple products using a multi-dimensional one. The way we |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 305 | // determine the array type is by looking for a required array key named "id". |
| 306 | // If it's not found we assume it's a multi-dimensional array |
| 307 | $save_cart = FALSE; |
| 308 | if (isset($items['rowid']) AND isset($items['qty'])) |
| 309 | { |
| 310 | if ($this->_update($items) == TRUE) |
| 311 | { |
| 312 | $save_cart = TRUE; |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | foreach ($items as $val) |
| 318 | { |
| 319 | if (is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) |
| 320 | { |
| 321 | if ($this->_update($val) == TRUE) |
| 322 | { |
| 323 | $save_cart = TRUE; |
| 324 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | // Save the cart data if the insert was successful |
| 330 | if ($save_cart == TRUE) |
| 331 | { |
| 332 | $this->_save_cart(); |
| 333 | return TRUE; |
| 334 | } |
| 335 | |
| 336 | return FALSE; |
| 337 | } |
| 338 | |
| 339 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 341 | /** |
| 342 | * Update the cart |
| 343 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | * This function permits the quantity of a given item to be changed. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 345 | * Typically it is called from the "view cart" page if a user makes |
| 346 | * changes to the quantity before checkout. That array must contain the |
| 347 | * product ID and quantity for each item. |
| 348 | * |
| 349 | * @access private |
| 350 | * @param array |
| 351 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | */ |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 353 | function _update($items = array()) |
| 354 | { |
| 355 | // Without these array indexes there is nothing we can do |
| 356 | if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']])) |
| 357 | { |
| 358 | return FALSE; |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 361 | // Prep the quantity |
| 362 | $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']); |
| 363 | |
| 364 | // Is the quantity a number? |
| 365 | if ( ! is_numeric($items['qty'])) |
| 366 | { |
| 367 | return FALSE; |
| 368 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 370 | // Is the new quantity different than what is already saved in the cart? |
| 371 | // If it's the same there's nothing to do |
| 372 | if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) |
| 373 | { |
| 374 | return FALSE; |
| 375 | } |
| 376 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 377 | // 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] | 378 | // If the quantity is greater than zero we are updating |
| 379 | if ($items['qty'] == 0) |
| 380 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | unset($this->_cart_contents[$items['rowid']]); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 382 | } |
| 383 | else |
| 384 | { |
| 385 | $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; |
| 386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 388 | return TRUE; |
| 389 | } |
| 390 | |
| 391 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 392 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 393 | /** |
| 394 | * Save the cart array to the session DB |
| 395 | * |
| 396 | * @access private |
| 397 | * @return bool |
| 398 | */ |
| 399 | function _save_cart() |
| 400 | { |
Rick Ellis | af4fb22 | 2009-02-24 22:44:22 +0000 | [diff] [blame] | 401 | // Unset these so our total can be calculated correctly below |
| 402 | unset($this->_cart_contents['total_items']); |
| 403 | unset($this->_cart_contents['cart_total']); |
| 404 | |
| 405 | // Lets add up the individual prices and set the cart sub-total |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 406 | $total = 0; |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 407 | $items = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 408 | foreach ($this->_cart_contents as $key => $val) |
| 409 | { |
| 410 | // We make sure the array contains the proper indexes |
Rick Ellis | af4fb22 | 2009-02-24 22:44:22 +0000 | [diff] [blame] | 411 | if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 412 | { |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | $total += ($val['price'] * $val['qty']); |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 417 | $items += $val['qty']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 419 | // Set the subtotal |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 420 | $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] | 421 | } |
| 422 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 423 | // Set the cart total and total items. |
MarcosCoelho | 98b2126 | 2011-07-18 16:12:47 -0300 | [diff] [blame] | 424 | $this->_cart_contents['total_items'] = $items; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 425 | $this->_cart_contents['cart_total'] = $total; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 427 | // Is our cart empty? If so we delete it from the session |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 428 | if (count($this->_cart_contents) <= 2) |
| 429 | { |
| 430 | $this->CI->session->unset_userdata('cart_contents'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 432 | // Nothing more to do... coffee time! |
| 433 | return FALSE; |
| 434 | } |
| 435 | |
| 436 | // If we made it this far it means that our cart has data. |
| 437 | // Let's pass it to the Session class so it can be stored |
| 438 | $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents)); |
| 439 | |
| 440 | // Woot! |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | return TRUE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 446 | /** |
| 447 | * Cart Total |
| 448 | * |
| 449 | * @access public |
| 450 | * @return integer |
| 451 | */ |
| 452 | function total() |
| 453 | { |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 454 | return $this->_cart_contents['cart_total']; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 459 | /** |
| 460 | * Total Items |
| 461 | * |
| 462 | * Returns the total item count |
| 463 | * |
| 464 | * @access public |
| 465 | * @return integer |
| 466 | */ |
| 467 | function total_items() |
| 468 | { |
| 469 | return $this->_cart_contents['total_items']; |
| 470 | } |
| 471 | |
| 472 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 474 | /** |
| 475 | * Cart Contents |
| 476 | * |
| 477 | * Returns the entire cart array |
| 478 | * |
| 479 | * @access public |
| 480 | * @return array |
| 481 | */ |
| 482 | function contents() |
| 483 | { |
| 484 | $cart = $this->_cart_contents; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 486 | // Remove these so they don't create a problem when showing the cart table |
| 487 | unset($cart['total_items']); |
| 488 | unset($cart['cart_total']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 490 | return $cart; |
| 491 | } |
| 492 | |
| 493 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 494 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Has options |
| 497 | * |
| 498 | * Returns TRUE if the rowid passed to this function correlates to an item |
| 499 | * that has options associated with it. |
| 500 | * |
| 501 | * @access public |
| 502 | * @return array |
| 503 | */ |
| 504 | function has_options($rowid = '') |
| 505 | { |
| 506 | if ( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) |
| 507 | { |
| 508 | return FALSE; |
| 509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 511 | return TRUE; |
| 512 | } |
| 513 | |
| 514 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 516 | /** |
| 517 | * Product options |
| 518 | * |
| 519 | * Returns the an array of options, for a particular product row ID |
| 520 | * |
| 521 | * @access public |
| 522 | * @return array |
| 523 | */ |
| 524 | function product_options($rowid = '') |
| 525 | { |
| 526 | if ( ! isset($this->_cart_contents[$rowid]['options'])) |
| 527 | { |
| 528 | return array(); |
| 529 | } |
| 530 | |
| 531 | return $this->_cart_contents[$rowid]['options']; |
| 532 | } |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 533 | |
| 534 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 536 | /** |
| 537 | * Format Number |
| 538 | * |
| 539 | * Returns the supplied number with commas and a decimal point. |
| 540 | * |
| 541 | * @access public |
| 542 | * @return integer |
| 543 | */ |
| 544 | function format_number($n = '') |
| 545 | { |
| 546 | if ($n == '') |
| 547 | { |
| 548 | return ''; |
| 549 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 551 | // Remove anything that isn't a number or decimal point. |
| 552 | $n = trim(preg_replace('/([^0-9\.])/i', '', $n)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 554 | return number_format($n, 2, '.', ','); |
| 555 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 556 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 557 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 558 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 559 | /** |
| 560 | * Destroy the cart |
| 561 | * |
| 562 | * Empties the cart and kills the session |
| 563 | * |
| 564 | * @access public |
| 565 | * @return null |
| 566 | */ |
| 567 | function destroy() |
| 568 | { |
| 569 | unset($this->_cart_contents); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 570 | |
| 571 | $this->_cart_contents['cart_total'] = 0; |
| 572 | $this->_cart_contents['total_items'] = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 573 | |
Rick Ellis | 9c86ce5 | 2009-02-17 19:54:14 +0000 | [diff] [blame] | 574 | $this->CI->session->unset_userdata('cart_contents'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | |
| 578 | } |
| 579 | // END Cart Class |
| 580 | |
| 581 | /* End of file Cart.php */ |
| 582 | /* Location: ./system/libraries/Cart.php */ |