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