Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 39 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Shopping Cart Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Shopping Cart |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/cart.html |
Andrey Andreev | 21c3c22 | 2014-12-04 12:10:00 +0200 | [diff] [blame] | 48 | * @deprecated 3.0.0 This class is too specific for CI. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 49 | */ |
| 50 | class CI_Cart { |
| 51 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 52 | /** |
| 53 | * These are the regular expression rules that we use to validate the product ID and product name |
| 54 | * alpha-numeric, dashes, underscores, or periods |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 55 | * |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 56 | * @var string |
| 57 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 58 | public $product_id_rules = '\.a-z0-9_-'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 59 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 60 | /** |
| 61 | * These are the regular expression rules that we use to validate the product ID and product name |
| 62 | * alpha-numeric, dashes, underscores, colons or periods |
| 63 | * |
| 64 | * @var string |
| 65 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 66 | public $product_name_rules = '\w \-\.\:'; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 67 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 68 | /** |
| 69 | * only allow safe product names |
| 70 | * |
| 71 | * @var bool |
| 72 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 73 | public $product_name_safe = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 74 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 75 | // -------------------------------------------------------------------------- |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 76 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 77 | /** |
| 78 | * Reference to CodeIgniter instance |
| 79 | * |
| 80 | * @var object |
| 81 | */ |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 82 | protected $CI; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 83 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 84 | /** |
| 85 | * Contents of the cart |
| 86 | * |
| 87 | * @var array |
| 88 | */ |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 89 | protected $_cart_contents = array(); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Shopping Class Constructor |
| 93 | * |
| 94 | * The constructor loads the Session class, used to store the shopping cart contents. |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 95 | * |
| 96 | * @param array |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 97 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 99 | public function __construct($params = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | { |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 101 | // Set the super object to a local variable for use later |
| 102 | $this->CI =& get_instance(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 104 | // Are any config settings being passed manually? If so, set them |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 105 | $config = is_array($params) ? $params : array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 107 | // Load the Sessions class |
Edwin Aw | edc9afa | 2013-01-25 16:12:20 +0800 | [diff] [blame] | 108 | $this->CI->load->driver('session', $config); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 110 | // Grab the shopping cart array from the session table |
| 111 | $this->_cart_contents = $this->CI->session->userdata('cart_contents'); |
Ivan Tcholakov | 29453cd | 2012-11-23 13:54:28 +0200 | [diff] [blame] | 112 | if ($this->_cart_contents === NULL) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 113 | { |
| 114 | // No cart exists so we'll set some base values |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 115 | $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 116 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 118 | log_message('info', 'Cart Class Initialized'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 123 | /** |
| 124 | * Insert items into the cart and save it to the session table |
| 125 | * |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 126 | * @param array |
| 127 | * @return bool |
| 128 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 129 | public function insert($items = array()) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 130 | { |
| 131 | // Was any cart data passed? No? Bah... |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 132 | if ( ! is_array($items) OR count($items) === 0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 133 | { |
| 134 | log_message('error', 'The insert method must be passed an array containing data.'); |
| 135 | return FALSE; |
| 136 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
| 138 | // You can either insert a single product using a one-dimensional array, |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 139 | // or multiple products using a multi-dimensional one. The way we |
| 140 | // determine the array type is by looking for a required array key named "id" |
| 141 | // 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] | 142 | |
| 143 | $save_cart = FALSE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 144 | if (isset($items['id'])) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | { |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 146 | if (($rowid = $this->_insert($items))) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 147 | { |
| 148 | $save_cart = TRUE; |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | foreach ($items as $val) |
| 154 | { |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 155 | if (is_array($val) && isset($val['id'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 156 | { |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 157 | if ($this->_insert($val)) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 158 | { |
| 159 | $save_cart = TRUE; |
| 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 | } |
| 163 | } |
| 164 | |
| 165 | // Save the cart data if the insert was successful |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 166 | if ($save_cart === TRUE) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 167 | { |
| 168 | $this->_save_cart(); |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 169 | return isset($rowid) ? $rowid : TRUE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return FALSE; |
| 173 | } |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Insert |
| 179 | * |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 180 | * @param array |
| 181 | * @return bool |
| 182 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 183 | protected function _insert($items = array()) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 184 | { |
| 185 | // Was any cart data passed? No? Bah... |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 186 | if ( ! is_array($items) OR count($items) === 0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 187 | { |
| 188 | log_message('error', 'The insert method must be passed an array containing data.'); |
| 189 | return FALSE; |
| 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 192 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 194 | // Does the $items array contain an id, quantity, price, and name? These are required |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 195 | if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 196 | { |
| 197 | log_message('error', 'The cart array must contain a product ID, quantity, price, and name.'); |
| 198 | return FALSE; |
| 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 203 | // Prep the quantity. It can only be a number. Duh... also trim any leading zeros |
Andrey Andreev | 17779d6 | 2011-12-22 13:21:08 +0200 | [diff] [blame] | 204 | $items['qty'] = (float) $items['qty']; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 205 | |
| 206 | // If the quantity is zero or blank there's nothing for us to do |
Andrey Andreev | 1a9b7e0 | 2012-11-01 16:23:47 +0200 | [diff] [blame] | 207 | if ($items['qty'] == 0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 208 | { |
| 209 | return FALSE; |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 212 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 214 | // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods |
| 215 | // Not totally sure we should impose this rule, but it seems prudent to standardize IDs. |
| 216 | // 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] | 217 | if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 218 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 219 | 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] | 220 | return FALSE; |
| 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 225 | // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods. |
| 226 | // Note: These can be user-specified by setting the $this->product_name_rules variable. |
Louis Racicot | 025b646 | 2013-03-07 09:32:16 -0500 | [diff] [blame] | 227 | if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 228 | { |
Andrew Seymour | 3dd6663 | 2011-12-13 15:50:52 +0000 | [diff] [blame] | 229 | 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'); |
| 230 | return FALSE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 235 | // Prep the price. Remove leading zeros and anything that isn't a number or decimal point. |
Andrey Andreev | 17779d6 | 2011-12-22 13:21:08 +0200 | [diff] [blame] | 236 | $items['price'] = (float) $items['price']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 238 | // 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] | 239 | // Every time something is added to the cart it is stored in the master cart array. |
| 240 | // Each row in the cart array, however, must have a unique index that identifies not only |
| 241 | // a particular product, but makes it possible to store identical products with different options. |
| 242 | // 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] | 243 | // 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] | 244 | // both sizes because it's the same shirt. The only difference will be the size. |
| 245 | // Internally, we need to treat identical submissions, but with different options, as a unique product. |
| 246 | // Our solution is to convert the options array to a string and MD5 it along with the product ID. |
| 247 | // This becomes the unique "row ID" |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 248 | if (isset($items['options']) && count($items['options']) > 0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 249 | { |
ThallisPHP | f074dff | 2012-05-03 16:01:46 -0300 | [diff] [blame] | 250 | $rowid = md5($items['id'].serialize($items['options'])); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
| 254 | // No options were submitted so we simply MD5 the product ID. |
| 255 | // Technically, we don't need to MD5 the ID in this case, but it makes |
| 256 | // sense to standardize the format of array indexes for both conditions |
| 257 | $rowid = md5($items['id']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 259 | |
| 260 | // -------------------------------------------------------------------- |
| 261 | |
| 262 | // 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] | 263 | // grab quantity if it's already there and add it on |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 264 | $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] | 265 | |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 266 | // Re-create the entry, just to make sure our index contains only the data from this submission |
| 267 | $items['rowid'] = $rowid; |
| 268 | $items['qty'] += $old_quantity; |
| 269 | $this->_cart_contents[$rowid] = $items; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Phil Sturgeon | 9091051 | 2011-07-20 10:07:40 -0600 | [diff] [blame] | 271 | return $rowid; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 276 | /** |
| 277 | * Update the cart |
| 278 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | * This function permits the quantity of a given item to be changed. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 280 | * Typically it is called from the "view cart" page if a user makes |
| 281 | * changes to the quantity before checkout. That array must contain the |
| 282 | * product ID and quantity for each item. |
| 283 | * |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 284 | * @param array |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 285 | * @return bool |
| 286 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 287 | public function update($items = array()) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 288 | { |
| 289 | // Was any cart data passed? |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 290 | if ( ! is_array($items) OR count($items) === 0) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 291 | { |
| 292 | return FALSE; |
| 293 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | |
| 295 | // You can either update a single product using a one-dimensional array, |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 296 | // or multiple products using a multi-dimensional one. The way we |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 297 | // determine the array type is by looking for a required array key named "rowid". |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 298 | // If it's not found we assume it's a multi-dimensional array |
| 299 | $save_cart = FALSE; |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 300 | if (isset($items['rowid'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 301 | { |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 302 | if ($this->_update($items) === TRUE) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 303 | { |
| 304 | $save_cart = TRUE; |
| 305 | } |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | foreach ($items as $val) |
| 310 | { |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 311 | if (is_array($val) && isset($val['rowid'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 312 | { |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 313 | if ($this->_update($val) === TRUE) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 314 | { |
| 315 | $save_cart = TRUE; |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | } |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | // Save the cart data if the insert was successful |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 322 | if ($save_cart === TRUE) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 323 | { |
| 324 | $this->_save_cart(); |
| 325 | return TRUE; |
| 326 | } |
| 327 | |
| 328 | return FALSE; |
| 329 | } |
| 330 | |
| 331 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 333 | /** |
| 334 | * Update the cart |
| 335 | * |
Ahmad Anbar | 7d16de6 | 2014-02-13 01:45:27 +0200 | [diff] [blame] | 336 | * This function permits changing item properties. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 337 | * Typically it is called from the "view cart" page if a user makes |
| 338 | * changes to the quantity before checkout. That array must contain the |
Ahmad Anbar | da2bdf2 | 2014-02-13 12:59:18 +0200 | [diff] [blame] | 339 | * rowid and quantity for each item. |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 340 | * |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 341 | * @param array |
| 342 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | */ |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 344 | protected function _update($items = array()) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 345 | { |
| 346 | // Without these array indexes there is nothing we can do |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 347 | if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 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 | // Prep the quantity |
Ahmad Anbar | 2702a3b | 2014-03-14 16:53:44 +0200 | [diff] [blame] | 353 | if (isset($items['qty'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 354 | { |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 355 | $items['qty'] = (float) $items['qty']; |
| 356 | // Is the quantity zero? If so we will remove the item from the cart. |
| 357 | // If the quantity is greater than zero we are updating |
| 358 | if ($items['qty'] == 0) |
| 359 | { |
| 360 | unset($this->_cart_contents[$items['rowid']]); |
Ahmad Anbar | 9af0746 | 2014-03-14 15:33:18 +0200 | [diff] [blame] | 361 | return TRUE; |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 362 | } |
Andrey Andreev | db3e49d | 2014-03-17 11:04:55 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 365 | // find updatable keys |
| 366 | $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items)); |
| 367 | // if a price was passed, make sure it contains valid data |
| 368 | if (isset($items['price'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 369 | { |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 370 | $items['price'] = (float) $items['price']; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 371 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 372 | |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 373 | // product id & name shouldn't be changed |
| 374 | foreach (array_diff($keys, array('id', 'name')) as $key) |
| 375 | { |
| 376 | $this->_cart_contents[$items['rowid']][$key] = $items[$key]; |
Andrey Andreev | db3e49d | 2014-03-17 11:04:55 +0200 | [diff] [blame] | 377 | } |
Ahmad Anbar | 4321cb0 | 2014-03-14 15:21:13 +0200 | [diff] [blame] | 378 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 379 | return TRUE; |
| 380 | } |
| 381 | |
| 382 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 383 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 384 | /** |
| 385 | * Save the cart array to the session DB |
| 386 | * |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 387 | * @return bool |
| 388 | */ |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 389 | protected function _save_cart() |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 390 | { |
vlakoff | 3567246 | 2013-02-15 01:36:04 +0100 | [diff] [blame] | 391 | // Let's add up the individual prices and set the cart sub-total |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 392 | $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 393 | foreach ($this->_cart_contents as $key => $val) |
| 394 | { |
| 395 | // We make sure the array contains the proper indexes |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 396 | if ( ! is_array($val) OR ! isset($val['price'], $val['qty'])) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 397 | { |
| 398 | continue; |
| 399 | } |
| 400 | |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 401 | $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']); |
| 402 | $this->_cart_contents['total_items'] += $val['qty']; |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 403 | $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] | 404 | } |
| 405 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 406 | // Is our cart empty? If so we delete it from the session |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 407 | if (count($this->_cart_contents) <= 2) |
| 408 | { |
| 409 | $this->CI->session->unset_userdata('cart_contents'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 411 | // Nothing more to do... coffee time! |
| 412 | return FALSE; |
| 413 | } |
| 414 | |
| 415 | // If we made it this far it means that our cart has data. |
| 416 | // Let's pass it to the Session class so it can be stored |
| 417 | $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents)); |
| 418 | |
Andrey Andreev | 17779d6 | 2011-12-22 13:21:08 +0200 | [diff] [blame] | 419 | // Woot! |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 420 | return TRUE; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 425 | /** |
| 426 | * Cart Total |
| 427 | * |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 428 | * @return int |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 429 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 430 | public function total() |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 431 | { |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 432 | return $this->_cart_contents['cart_total']; |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 433 | } |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 434 | |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 435 | // -------------------------------------------------------------------- |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 436 | |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 437 | /** |
| 438 | * Remove Item |
| 439 | * |
| 440 | * Removes an item from the cart |
| 441 | * |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 442 | * @param int |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 443 | * @return bool |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 444 | */ |
| 445 | public function remove($rowid) |
| 446 | { |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 447 | // unset & save |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 448 | unset($this->_cart_contents[$rowid]); |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 449 | $this->_save_cart(); |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 450 | return TRUE; |
Andrew Seymour | f75ec11 | 2011-12-14 09:36:39 +0000 | [diff] [blame] | 451 | } |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 452 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 453 | // -------------------------------------------------------------------- |
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 | /** |
| 456 | * Total Items |
| 457 | * |
| 458 | * Returns the total item count |
| 459 | * |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 460 | * @return int |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 461 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 462 | public function total_items() |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 463 | { |
| 464 | return $this->_cart_contents['total_items']; |
| 465 | } |
| 466 | |
| 467 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 468 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 469 | /** |
| 470 | * Cart Contents |
| 471 | * |
| 472 | * Returns the entire cart array |
| 473 | * |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame] | 474 | * @param bool |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 475 | * @return array |
| 476 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 477 | public function contents($newest_first = FALSE) |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 478 | { |
Andrew Seymour | de2e96a | 2011-12-13 16:44:59 +0000 | [diff] [blame] | 479 | // do we want the newest first? |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 480 | $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 482 | // Remove these so they don't create a problem when showing the cart table |
| 483 | unset($cart['total_items']); |
| 484 | unset($cart['cart_total']); |
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 | return $cart; |
| 487 | } |
| 488 | |
| 489 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 491 | /** |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 492 | * Get cart item |
| 493 | * |
| 494 | * Returns the details of a specific item in the cart |
| 495 | * |
| 496 | * @param string $row_id |
| 497 | * @return array |
| 498 | */ |
| 499 | public function get_item($row_id) |
| 500 | { |
| 501 | return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->_cart_contents[$row_id])) |
| 502 | ? FALSE |
| 503 | : $this->_cart_contents[$row_id]; |
| 504 | } |
| 505 | |
| 506 | // -------------------------------------------------------------------- |
| 507 | |
| 508 | /** |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 509 | * Has options |
| 510 | * |
| 511 | * Returns TRUE if the rowid passed to this function correlates to an item |
| 512 | * that has options associated with it. |
| 513 | * |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 514 | * @param string $row_id = '' |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 515 | * @return bool |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 516 | */ |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 517 | public function has_options($row_id = '') |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 518 | { |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 519 | return (isset($this->_cart_contents[$row_id]['options']) && count($this->_cart_contents[$row_id]['options']) !== 0); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 524 | /** |
| 525 | * Product options |
| 526 | * |
| 527 | * Returns the an array of options, for a particular product row ID |
| 528 | * |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 529 | * @param string $row_id = '' |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 530 | * @return array |
| 531 | */ |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 532 | public function product_options($row_id = '') |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 533 | { |
Andrey Andreev | cdeee66 | 2012-10-25 17:29:52 +0300 | [diff] [blame] | 534 | return isset($this->_cart_contents[$row_id]['options']) ? $this->_cart_contents[$row_id]['options'] : array(); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 535 | } |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 536 | |
| 537 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 539 | /** |
| 540 | * Format Number |
| 541 | * |
| 542 | * Returns the supplied number with commas and a decimal point. |
| 543 | * |
Andrey Andreev | 6f042cc | 2012-03-26 14:58:33 +0300 | [diff] [blame] | 544 | * @param float |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 545 | * @return string |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 546 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 547 | public function format_number($n = '') |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 548 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 549 | return ($n === '') ? '' : number_format( (float) $n, 2, '.', ','); |
Rick Ellis | df39d51 | 2009-02-24 22:29:37 +0000 | [diff] [blame] | 550 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 552 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 554 | /** |
| 555 | * Destroy the cart |
| 556 | * |
| 557 | * Empties the cart and kills the session |
| 558 | * |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 559 | * @return void |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 560 | */ |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 561 | public function destroy() |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 562 | { |
Andrey Andreev | bb24883 | 2011-12-21 16:42:51 +0200 | [diff] [blame] | 563 | $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0); |
Rick Ellis | 9c86ce5 | 2009-02-17 19:54:14 +0000 | [diff] [blame] | 564 | $this->CI->session->unset_userdata('cart_contents'); |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Rick Ellis | 9878332 | 2009-02-17 02:29:44 +0000 | [diff] [blame] | 567 | } |