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