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